mirror of https://github.com/apache/openjpa.git
OPENJPA-446
git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.x@596737 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9e014f54d9
commit
de1aae296e
openjpa-kernel/src/main/java/org/apache/openjpa/kernel
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/detachment
|
@ -552,7 +552,8 @@ public class DetachedStateManager
|
|||
public void settingStringField(PersistenceCapable pc, int idx, String cur,
|
||||
String next, int set) {
|
||||
accessingField(idx);
|
||||
if (cur == next || !_loaded.get(idx))
|
||||
if (cur == next || (cur != null && cur.equals(next))
|
||||
|| !_loaded.get(idx))
|
||||
return;
|
||||
lock();
|
||||
try {
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package org.apache.openjpa.persistence.detachment;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.apache.openjpa.persistence.Persistent;
|
||||
|
||||
@Entity
|
||||
public class Record {
|
||||
|
||||
@Persistent
|
||||
private String content;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package org.apache.openjpa.persistence.detachment;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
|
||||
public class TestDetachment extends SingleEMFTestCase {
|
||||
|
||||
private int id;
|
||||
|
||||
public void setUp() {
|
||||
super.setUp(Record.class);
|
||||
id = prepare();
|
||||
}
|
||||
|
||||
public void testAttachWithNewString() {
|
||||
// set up record with string "default" as content
|
||||
Record record = detach(id);
|
||||
|
||||
// set different text
|
||||
record.setContent("a text different than the one in the record");
|
||||
|
||||
attach(record);
|
||||
}
|
||||
|
||||
public void testSetSameStringInstance() {
|
||||
Record record = detach(id);
|
||||
|
||||
// same text, same String instance
|
||||
record.setContent(record.getContent());
|
||||
|
||||
attach(record);
|
||||
}
|
||||
|
||||
public void testSetSameString() {
|
||||
Record record = detach(id);
|
||||
// same text, different String instance
|
||||
record.setContent(record.getContent() + "");
|
||||
|
||||
attach(record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new record, sets the content to "default" and returns the id.
|
||||
*/
|
||||
private int prepare() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
|
||||
em.getTransaction().begin();
|
||||
Record record = new Record();
|
||||
record.setContent("default");
|
||||
em.persist(record);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
return record.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the record with the given id and returns a detached instance.
|
||||
*/
|
||||
private Record detach(int id) {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
Record record = em.find(Record.class, id);
|
||||
em.close(); // detach
|
||||
return record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the record into a new persistence context.
|
||||
*/
|
||||
private void attach(Record record) {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
record = em.merge(record);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue