I have a class called WebAsset:
public class WebAsset {
private Long id;
private String url;
private int status;
//more fields that are not relevent
}
I need to be able to show relationships between WebAsset, so I created a table for the relationship and a composite key class.
public class WebAssetReferencePK {
private Long sourceAssetId;
private Long targetAssetId;
}
public class WebAssetReference {
private WebAssetReferencePK wpk;
private Long updateTime;
}
We are forced to use an older version of Hibernate so we need to use xml files instead of annotaions. Here is the mapping for the reference class:
<class name="ca.gc.cra.www.crawler.valueobject.WebAssetReference" table="webassetreference">
<composite-id name="webAssetReferencePK" class="ca.gc.cra.www.crawler.valueobject.WebAssetReferencePK">
<key-property name="sourceAsset" type="java.lang.Long" column="sourceAssetId" />
<key-property name="targetAsset" type="java.lang.Long" column="targetAssetId" />
</composite-id>
<property name="updateTime" type="java.lang.Long" column="updatetime" not-null="true" />
</class>
In the composite key I get what I expect in the database with 2 ids related to each other. But when I try to query with HQL or Criteria it doesn't work since there is no direct relation between the PK class and WebAsset and I need to be able to do a join between WebAsset and WebAssetReference. If I try to change the composite key types from java.lang.Long to WebAsset then hibernate stores the whole object in the WebAssetReference table instead of just the ids.
An example of what I am trying to do is if I have a sourceAssetId I want to return all the targetAssetIds with the same source, but I don't want the ids themselves I want the WebAsset that is the primary key for each targetAssetId.
I have been searching around for the answer but every example I can find are just simple examples that don't relate.
Update 1: With continued searching I finally found the answer. Instead of key-property I need to use key-many-to-one. I haven't tried a join yet but everything else looks right so this should be the answer.
Update 2: Can't get the query to work with HQL. Here is th SQL of what I am trying to do:
select * from webasset as wa join webassetreference as war on war.targetassetid=wa.webasset_id where war.sourceassetid=?
Here is the HQL that is not working:
FROM WebAsset JOIN WebAssetReference WebAssetReference.WebAssetReferencePK.targetAsset=WebAsset WHERE WebAssetReference.WebAssetReferencePK.sourceAsset = :sourceAsset
I get the following error with HQL: ERROR - line 1:89: unexpected token: .
I'll keep trying but I can't seem to figure out the HQL.
I discovered how to do this. In the case I have above it will not work since I have 2 columns joining to the same table. However if I use the same WebAsset class above and instead use this class:
public class TreeNode implements Comparable<TreeNode>{
private String nodeUrl;
private Long id;
private Boolean folder;
private transient WebAsset nodeAsset = null;
}
With this .hbm.xml file:
<class name="ca.gc.cra.www.crawler.valueobject.TreeNode" table="TreeNode">
<id name="id" type="java.lang.Long" column="treenode_id" >
<generator class="identity"/>
</id>
<many-to-one name="nodeAsset" class="ca.gc.cra.www.crawler.valueobject.WebAsset" column="nodeAsset_id" lazy="false" not-null="false" cascade="none" unique="true" />
<property name="folder" type="java.lang.Boolean" column="folder" not-null="true" />
<property name="nodeUrl" length="512" type="java.lang.String" column="nodeUrl" not-null="true" />
<set name="children" table="TreeNode" inverse="false" lazy="true" >
<key column="parentnode_id"/>
<one-to-many class="ca.gc.cra.www.crawler.valueobject.TreeNode" />
</set>
</class>
You can then use this code to retrieve the join:
Session session = HibernateUtil.getSession();
try {
String hql = "FROM TreeNode tn JOIN tn.nodeAsset WHERE tn.id=5";
Query query = session.createQuery(hql);
List result = query.list();
System.out.println("done");
} catch (HibernateException e) {
e.printStackTrace();
throw new Exception("Query failed", e);
} finally {
session.flush();
session.close();
}
Hibernate can then perform the join correctly. The result will be a List containing an Object array for each entry. The Object contains the 2 classes that are part of the join. You have to cast the Object with (Object[]) to access the elements and then cast each on to the appropriate class.
I would recommend against this approach because Hibernate will attempt to load all connected classes as well. With the example above I was getting 1 row from TreeNode yet it generated 19 select statements. I even attempted to set the connected classes to lazy load and it still generated all the selects.
Related
I'm unable to delete a child record while I'm updating (not deleting) the parent record. Also, I've read other posts, but it seems most of the others are using annotations rather than xml, so it can be difficult to see how they relate to my issue.
I have two tables: The EventInfo table that holds information about events and then the EventLicenseType table that only has two columns and both of those columns make up the primary key; one of the columns in the EventLicenseType table is a foreign key to the EventInfo table.
The problem is I can't seem to delete an EventLicenseType record. I've tried a bunch of different things and nothing is working for me. It seems like hibernate wants to put null as the eventinfoId column, which of course doesn't work. I have tried clearing out the Set and then doing the merge, and also specifically calling session.delete(eventlicenseTypeRec) and then doing the merge. Neither is working for me.
EventInfo.hbm.xml file:
<hibernate-mapping default-lazy="true">
<class name="Eventinfo" table="PA_EVENTINFO">
<id name="eventInfoId" type="int"
column="PA_EVENTINFOID">
<generator class="native" />
</id>
<property name="eventTypeId" type="java.lang.String"
column="PA_EVENTTYPEID" length="255" />
...Other columns not shown here for brevity...
<set name="eventLicenceTypeIds" lazy="false" cascade="all-delete-orphan">
<key column="PA_EVENTINFOID"/>
<one-to-many class="EventLicenseType" />
</set>
</class>
EventLicenseType.hbm.xml file:
<hibernate-mapping default-lazy="true">
<class name="EventLicenseType" table="PA_EVENTLICENSETYPE">
<composite-id>
<key-property name="licenseTypeId" type="java.lang.Integer" column="PA_LICENSETYPE"/>
<key-property name="eventInfoId" type="java.lang.Integer" column="PA_EVENTINFOID"/>
</composite-id>
</class>
Here is the EventInfo class. Again, there are more fields in the actual file, this is just the important pieces:
public class Eventinfo implements Serializable {
/** identifier field */
private int eventInfoId;
/** nullable persistent field */
#Field(name="eventInfo_eventTypeId")
private String eventTypeId;
#IndexedEmbedded
private Set<EventLicenseType> eventLicenceTypeIds;
/** default constructor */
public Eventinfo() {}
public int getEventInfoId() {
return this.eventInfoId;
}
public void setEventInfoId(int eventInfoId) {
this.eventInfoId = eventInfoId;
}
public String getEventTypeId() {
return this.eventTypeId;
}
public void setEventTypeId(String eventTypeId) {
this.eventTypeId = eventTypeId;
}
public Set<EventLicenseType> getEventLicenceTypeIds() {
return eventLicenceTypeIds;
}
public void setEventLicenceTypeIds(Set<EventLicenseType> eventLicenceTypeIds) {
this.eventLicenceTypeIds = eventLicenceTypeIds;
}
Here is the EventLicenseType class
public class EventLicenseType implements Serializable{
#Field
private int licenseTypeId;
private int eventInfoId;
public int getLicenseTypeId() {
return licenseTypeId;
}
public void setLicenseTypeId(int licenseTypeId) {
this.licenseTypeId = licenseTypeId;
}
public int getEventInfoId() {
return eventInfoId;
}
public void setEventInfoId(int eventInfoId) {
this.eventInfoId = eventInfoId;
}
}
Here is the method I'm executing in my DAO. For now there is only one record associated to the eventInfo record, so I'm just trying to see if I can delete that one. (Also note that eventinfo is defined in the method that surrounds this one).
public Eventinfo execute(Session session) throws Exception {
//Get the existing eventInfo record
Eventinfo existing = (Eventinfo)session.get(Eventinfo.class, eventinfo.getEventInfoId());
Iterator iter = existing.getEventLicenceTypeIds().iterator();
if (iter.hasNext()) {
EventLicenseType license = (EventLicenseType) iter.next();
iter.remove();
session.delete(license);
}
session.flush();
return (Eventinfo) session.merge(eventinfo);
}
On the above session.flush() line, I get an error: java.sql.BatchUpdateException: Cannot insert the value NULL into column 'PA_EVENTINFOID', table 'PA_EVENTLICENSETYPE'; column does not allow nulls. UPDATE fails. It shows that hibernate is trying to do:
update PA_EVENTLICENSETYPE set PA_EVENTINFOID=null where PA_EVENTINFOID=?
Why can't it just delete the record? Why is it trying to do an update??
I also tried changing the code to the below and get the same error.
public Eventinfo execute(Session session) throws Exception {
//Clear out the list
eventinfo.getEventLicenceTypeIds().clear();
return (Eventinfo) session.merge(eventinfo);
}
Can anyone help me with what I'm missing, or point me in the right direction?
You need to see whether the mapping between two tables are unilateral or bilateral. Basically you need to think or the rows of two tables as objects and cut all the ties between the objects of EventInfo and EventLicenceType tables. So if the relation is only from EventInfo -> EventLicenseType, you need to set the value of the EventLicenseType set to null in EventInfo object. Also if there is a mapping from EventLicenseType, you need to set the value of joining column to null. Then merge() the EventInfo object.
Noneed to explicitly delete or remove the EventLicenseType object. If here are no references to EventLicenseType object, the JVM will collect it as garbage.
Hope that solves your problem.
I am new to the hibernate world and I am getting the following error message when trying to execute a query with hibernate and postgres.
org.postgresql.util.PSQLException: ERROR: operator does not exist: text = bytea
Hint: No operator matches the given name and argument type(s). You might
need to add explicit type casts.
Here is my hibernate mapping (car.hbm.xml):
<hibernate-mapping>
<class name="Car" table="car"
schema="someSchema">
<id name="id" type="int" column="car_id">
<generator class="sequence">
<param name="sequence">car_seq</param>
</generator>
</id>
<property name="carMake">
<column name="car_make" sql-type="string"/>
</property>
<property name="carModel">
<column name="car_model" sql-type="string"/>
</property>
<property name="carVin" >
<column name="car_vin" sql-type="int" />
</property>
<property name="datePurchased">
<column name="date_purchased" sql-type="date"/>
</property>
<property name="retiredModel">
<column name="retired_model" sql-type="boolean"/>
</property>
</class>
On Postgres, here is what my table looks like:
CREATE TABLE car (
car_vin INTEGER NOT NULL DEFAULT nextval('car_seq'::regclass) PRIMARY KEY,
car_make TEXT NOT NULL,
car_model TEXT DEFAULT NULL,
date_purchased DATE DEFAULT now() NOT NULL,
retired_model BOOLEAN DEFAULT FALSE NOT NULL
);
Here is my model class (Car.java):
public class Car {
private int id;
private String carMake;
private String carModel;
private int carVin;
private Date datePurchased;
private boolean retiredModel;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCarModel() {
return carModel;
}
public void setcarModel(String carModel) {
this.carModel = carModel;
}
public String getcarMake() {
return carMake;
}
public void setcarMake(String carMake) {
this.carMake = carMake;
}
public Date getDatePurchased() {
return datePurchased;
}
public void setDatePurchased(Date datePurchased) {
this.datePurchased = datePurchased;
}
public boolean isRetired() {
return retiredModel;
}
public void setRetired(boolean retiredModel) {
this.retiredModel = retiredModel;
}
In my DAO layer, I am using the following line to query:
Query query = getSession().createQuery("from Car as c where " +
"c.carModel = ? AND c.carMake = ?").setParameter(0, carModel).setParameter(1, carMake);
carMake and carModel are both String datatypes passed on as method parameters in the DAO method.
Note that the strings in my hbm are mapped to TEXT in postgres, so I am guessing if that is the problem or not. If it is, how do I solve it ?
It is weird but the query does not handle null very well. When I changed the query to:
Query query = getSession().createQuery("from Car as c where " +
"c.carModel = ? AND c.carMake is null").setParameter(0, carModel);
it works fine since the DAO needs to query the make as NULL. So if it is not null, I need to have two sets of query, one that is hardcoded to select null as above, other to setParam(1, carMake).
Weird but I think this works.
Usually this error is from Hibernate serializing a not-otherwise-mapped class (resulting in a bytea) and comparing that to a String (probably given by you in a query).
Map the Date! Use #Temporal(Date) on the Date attribute. I don't know how to express that in hbm.xml notation.
Use Query.setParameterList instead of setParameter solved my problem for an integer array (integer = bytea)
I had the same problem and in my case it was caused by the fact that I was trying to use a Java enum class as a named parameter within a native query.
The solution was to go all the way with Hibernate, so non-native query and using Java class and member names instead of table name and column names.
I am running into a NucleusUserException while querying an entity from DB. I have tried a long time and couldn't figure out what caused this problem. I am hoping some of you can point me into the right direction. Any help will be greatly appreciated
Entity Class
#PersistenceCapable(detachable = "true")
public class Position implements IsSerializable, Serializable {
#PrimaryKey
#Persistent
protected String key;
#Persistent
protected Double quantity;
// getter and setter omitted
}
JDO MetaData
<jdo xmlns="http://java.sun.com/xml/ns/jdo/jdo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdo
http://java.sun.com/xml/ns/jdo/jdo_3_0.xsd">
<package name="org.sly.main.shared.data.finance.trading">
<class name="Position" detachable="true" cacheable="false">
<version strategy="version-number" />
<field name="key" persistence-modifier="persistent"
value-strategy="increment" primary-key="true">
<column length="32" jdbc-type="VARCHAR" />
</field>
<field name="quantity" persistence-modifier="persistent" />
</class>
</package>
</jdo>
The code that i used to retrieved
PersistenceManager pm = pmf.getPersistenceManager();
Extent e = pm.getExtent(Position.class, true);
Query query = pm.newQuery(e);
query.setFilter("key == my_key");
query.declareParameters("String my_key");
Object[] params = { "1234" };
List<Position> managerList = (List<Position>) query.executeWithArray(params);
The Exception i Got is below:
Cannot find mapping for field org.sly.main.shared.data.finance.trading.Position.key in table `POSITION` [`POSITION`.`KEY`, `POSITION`.`QUANTITY`]
org.datanucleus.exceptions.NucleusUserException: Cannot find mapping for field org.sly.main.shared.data.finance.trading.Position.key in table `POSITION` [`POSITION`.`KEY`, `POSITION`.`QUANTITY`]
at org.datanucleus.store.rdbms.table.AbstractClassTable.addApplicationIdUsingClassTableId(AbstractClassTable.java:233)
at org.datanucleus.store.rdbms.table.ClassTable.initializePK(ClassTable.java:1031)
at org.datanucleus.store.rdbms.table.ClassTable.preInitialize(ClassTable.java:246)
at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.addClassTable(RDBMSStoreManager.java:3146)
at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.addClassTables(RDBMSStoreManager.java:2937)
at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.addClassTablesAndValidate(RDBMSStoreManager.java:3210)
at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.run(RDBMSStoreManager.java:2869)
at org.datanucleus.store.rdbms.AbstractSchemaTransaction.execute(AbstractSchemaTransaction.java:122)
at org.datanucleus.store.rdbms.RDBMSStoreManager.addClasses(RDBMSStoreManager.java:1606)
at org.datanucleus.store.AbstractStoreManager.addClass(AbstractStoreManager.java:954)
at org.datanucleus.store.AbstractStoreManager.getSubClassesForClass(AbstractStoreManager.java:1693)
at org.datanucleus.store.rdbms.sql.DiscriminatorStatementGenerator.getStatement(DiscriminatorStatementGenerator.java:306)
at org.datanucleus.store.rdbms.scostore.JoinListStore.getIteratorStatement(JoinListStore.java:964)
at org.datanucleus.store.rdbms.scostore.JoinListStore.listIterator(JoinListStore.java:691)
at org.datanucleus.store.rdbms.scostore.AbstractListStore.listIterator(AbstractListStore.java:92)
at org.datanucleus.store.rdbms.scostore.AbstractListStore.iterator(AbstractListStore.java:82)
at org.datanucleus.store.types.backed.ArrayList.loadFromStore(ArrayList.java:294)
at org.datanucleus.store.types.backed.ArrayList.initialise(ArrayList.java:243)
at org.datanucleus.store.types.SCOUtils.createSCOWrapper(SCOUtils.java:256)
at org.datanucleus.store.types.SCOUtils.newSCOInstance(SCOUtils.java:142)
at org.datanucleus.store.rdbms.mapping.java.AbstractContainerMapping.replaceFieldWithWrapper(AbstractContainerMapping.java:399)
at org.datanucleus.store.rdbms.mapping.java.AbstractContainerMapping.postFetch(AbstractContainerMapping.java:417)
at org.datanucleus.store.rdbms.request.FetchRequest.execute(FetchRequest.java:420)
at org.datanucleus.store.rdbms.RDBMSPersistenceHandler.fetchObject(RDBMSPersistenceHandler.java:324)
at org.datanucleus.state.AbstractStateManager.loadFieldsFromDatastore(AbstractStateManager.java:1122)
at org.datanucleus.state.JDOStateManager.loadUnloadedFieldsInFetchPlan(JDOStateManager.java:3000)
at org.datanucleus.state.JDOStateManager.isLoaded(JDOStateManager.java:3214)
Not sure will this help but this is run on AWS Elastic Mapreduce Hadoop cluster
I'm having trouble with my hibernate transaction. After execution , table has no values in it and is not updated at all. All my other updates work properly.
Here's the mapping
<class name="LastDownloadedMessage" table="t_imap_lastmsguid">
<id name="id" column="id" ><generator class="increment"/></id>
<property name="lastDownloadedMessageUid"><column name="last_msg_uid" /></property>
<property name="lastUidNext"><column name="next_msg_uid" /></property>
<property name="folder"><column name="folder_name" /></property>
<property name="cred"><column name="credential" /></property>
</class>
This is the POJO object :
public class LastDownloadedMessage {
Integer id;
private String lastDownloadedMessageUid;
private String lastUidNext;
private String folder;
private String cred;
//GETTERS AND SETTERS HERE
public LastDownloadedMessage() {
super();
}
public LastDownloadedMessage(String lastDownloadedMessageUid,
String lastUidNext) {
super();
this.lastDownloadedMessageUid = lastDownloadedMessageUid;
this.lastUidNext = lastUidNext;
}
}
and this is the function which is doing the update.
Session ssn=HibernateSessionFactory.openSession();
Transaction txn = ssn.beginTransaction();
Query query = ssn.createQuery("update LastDownloadedMessage e set e.lastDownloadedMessageUid = :luid , e.lastUidNext = :nextUid where e.folder =:folder and e.cred = :cred");
query.setParameter("luid",last_downloaded_msg_uid);
query.setParameter("nextUid", uid_next);
query.setParameter("folder", folder);
query.setParameter("cred", credential);
int result = query.executeUpate();
txn.commit();
ssn.flush();
ssn.close();
The function appears to execute properly with no errors . What could be the issue ?
Based on the comments, it seems to me you are confusing UPDATE with INSERT.
If result is zero, it means your WHERE clause didn't match anything, so there's nothing to update.
Also, why are you trying to issue an UPDATE/INSERT on a known (mapped) entity? All you have to do is set your values on your object (LastDownloadedMessage) and then execute a persist on your EntityManager. Browse around the web for EntityManager.persist.
I know that deleting orphaned child objects is a common question on SO and a common problem for people new to Hibernate, and that the fairly standard answer is to ensure that you have some variation of cascade=all,delete-orphan or cascade=all-delete-orphan on the child collection.
I'd like to be able to have Hibernate detect that child collection has been emptied/removed from the parent object, and have the rows in the child table deleted from the database when the parent object is updated. For example:
Parent parent = session.get(...);
parent.getChildren().clear();
session.update(parent);
My current mapping for the Parent class looks like:
<bag name="children" cascade="all-delete-orphan">
<key column="parent_id" foreign-key="fk_parent_id"/>
<one-to-many class="Child"/>
</bag>
This works fine for me when updating an attached object, but I have a use case in which we'd like to be able to take a detached object (which has been sent to our API method by a remote client over HTTP/JSON), and pass it directly to the Hibernate Session - to allow clients to be able to manipulate the parent object in whichever way they like and have the changes persisted.
When calling session.update(parent) on my detached object, the rows in the child table are orphaned (the FK column is set to null) but not deleted. Note that when I'm calling session.update(), this is the first time the Hibernate Session is seeing this object instance - I am not re-attaching or merging the object with the Session in any other way. I'm relying on the client to pass objects whose identifiers correspond to actual objects in the database. For example, the logic in my API service method is something like this:
String jsonString = request.getParameter(...);
Parent parent = deserialize(jsonString);
session.update(parent);
Is it possible for Hibernate to detect orphaned children collections in detached parent objects when passed to session.update(parent)? Or am I mis-using the detached object in some way?
My hope was that I could avoid any sort of complex interactions with Hibernate to persist changes to a detached instance. My API method has no need to further modify the detached object after the call to session.update(parent), this method is merely responsible for persisting changes made by remote client applications.
Your mapping (simplified)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="br.com._3988215.model.domain">
<class name="Parent" table="PARENT">
<id name="id">
<generator class="native"/>
</id>
<bag cascade="all,delete-orphan" name="childList">
<key column="PARENT_ID" not-null="false"/>
<one-to-many class="Child"/>
</bag>
</class>
<class name="Child" table="CHILD">
<id name="id" column="CHILD_ID">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
produces
PARENT
ID
CHILD
CHILD_ID
PARENT_ID
According what you said
I would like to be able to have Hibernate detect that child collection has been removed from the parent object, and have the rows in the child table deleted from the database when the parent object is updated
Something like
Parent parent = session.get(...);
parent.getChildren().clear();
session.update(parent);
You said it works fine because you have an attached Parent instance
Now let's see the following one (Notice Assert.assertNull(second))
public class WhatYouWantTest {
private static SessionFactory sessionFactory;
private Serializable parentId;
private Serializable firstId;
private Serializable secondId;
#BeforeClass
public static void setUpClass() {
Configuration c = new Configuration();
c.addResource("mapping.hbm.3988215.xml");
sessionFactory = c.configure().buildSessionFactory();
}
#Before
public void setUp() throws Exception {
Parent parent = new Parent();
Child first = new Child();
Child second = new Child();
Session session = sessionFactory.openSession();
session.beginTransaction();
parentId = session.save(parent);
firstId = session.save(first);
secondId = session.save(second);
parent.getChildList().add(first);
parent.getChildList().add(second);
session.getTransaction().commit();
session.close();
}
#Test
public void removed_second_from_parent_remove_second_from_database() {
Parent parent = new Parent();
parent.setId((Integer) parentId);
Child first = new Child();
first.setId((Integer) firstId);
/**
* It simulates the second one has been removed
*/
parent.getChildList().add(first);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.update(parent);
session.getTransaction().commit();
session.close();
session = sessionFactory.openSession();
session.beginTransaction();
Child second = (Child) session.get(Child.class, secondId);
Assert.assertNull(second);
session.getTransaction().commit();
session.close();
}
}
Unfortunately, the test do not pass. What you can do ???
Enable a long-running conversation
Hibernate reference says
Extended (or Long) Session - The Hibernate Session may be disconnected from the underlying JDBC connection after the database transaction has been committed, and reconnected when a new client request occurs. This pattern is known as session-per-conversation and makes even reattachment unnecessary. Automatic versioning is used to isolate concurrent modifications and the Session is usually not allowed to be flushed automatically, but explicitely.
disclaimer: i do not have any scenario which uses long running conversation. Java EE Stateful session beans support long running conversation. But its support is for JPA (not Hibernate)
Or you can create an alternative mapping which enables your Child as composite elements. Because its lifecycle depends on the parent object, you can rely on composite elements to get what you want
Create a class named AlternativeParent which extends Parent
public class AlternativeParent extends Parent {}
Now its mapping (Notice Child as composite element instead of plain #Entity)
<class name="AlternativeParent" table="PARENT">
<id name="id">
<generator class="native"/>
</id>
<bag name="childList" table="CHILD">
<key column="PARENT_ID" not-null="false"/>
<composite-element class="Child">
<property column="CHILD_ID" name="id"/>
</composite-element>
</bag>
</class>
Now implement a convenient equals method in the Child class
public boolean equals(Object o) {
if (!(o instanceof Child))
return false;
Child other = (Child) o;
// identity equality
// Used by composite elements
if(getId() != null) {
return new EqualsBuilder()
.append(getId(), other.getId())
.isEquals();
} else {
// object equality
}
}
If i refactor the test case shown above (Now by using AlternativeParent instead)
#Test
public void removed_second_from_parent_remove_second_from_database() {
AlternativeParent parent = new AlternativeParent();
parent.setId((Integer) parentId);
Child first = new Child();
first.setId((Integer) firstId);
/**
* It simulates the second one has been removed
*/
parent.getChildList().add(first);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.update(parent);
session.getTransaction().commit();
session.close();
session = sessionFactory.openSession();
session.beginTransaction();
Child second = (Child) session.get(Child.class, secondId);
Assert.assertNull(second);
session.getTransaction().commit();
session.close();
}
I see a green bar
i think,when using detached session, you might face problem, with collections. i will suggest you to first load the entity with collection, and then update that entity with the changes, that will help.