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.
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 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.
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 am running into a problem managing my Hibernate-backed collections. The problem is illustrated in the code below; briefly,
A single session and transaction are started.
All instances of ClassA are loaded and walked to force Hibernate to load all instances of ClassC. At this point all instances of ClassC actually load via lazy fetching.
In the same transaction and session, all instances of ClassB are loaded.
All instances of ClassC are no longer available (the containing collections are empty) and do not lazy load if requested.
What's the best way to load up the data sets needed and keep them in memory?
Notes:
The project is in rapid-prototyping mode so I am using a single session and single
transaction to load up two big chunks of the application's data. This is also a desktop application so the use of a single session and transaction may not even be a problem in the long run if the code were working.
I set up ehcache and the debugger showing that Hibernate found the ehcache config file. The cache is set such that
maxElementsInMemory="5000000"
eternal="true"
the timeouts are set to 20 mins, even though eternal is set true, just to be sure the session isn't timing out.
The data set size is not particularly large, about 100k records.
The Hibernate 3.0 module packaged with NetBeans was use to create the class templates from a data model.
The mapping file looks roughly like this:
<hibernate-mapping>
<class catalog="myCatalog" name="ClassA" table="classA">
<id ... </id>
<set inverse="true" name="classCs" sort="natural">
<key>
<column length="12" name="mykey" not-null="true"/>
</key>
<one-to-many class="ClassC"/>
</set>
</class>
</hibernate-mapping>
Class definitions:
#Entity
#Table(name = "classA", catalog = "myCatalog")
public class ClassA implements java.io.Serializable {
private SortedSet<ClassC> classCs = new TreeSet<ClassC>();
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "ClassA")
public SortedSet<ClassC> getClassCs() {
return this.classCs;
}
}
#Entity
#Table(name = "classB", catalog = "myCatalog")
public class ClassB implements java.io.Serializable {
// ... It is possible to walk collections in this class to reach some instances of classA.
}
public class ClassC implements java.io.Serializable {
// ... contains no collections
}
Manifestation of problem:
// in Hibernate Util
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
public static void main(String[] args) {
Application.session = HibernateUtil.getSessionFactory().openSession();
Application.tx = session.beginTransaction();
// Load data set 'A' and force collections to load
List<ClassA> listClassA = new ArrayList<ClassA>();
Query q = Application.session.createQuery("from ClassA as a").setReadOnly(true);
Iterator<ClassA> iterator = q.list().iterator();
while (iterator.hasNext()) {
ClassA ca = (ClassA)iterator.next();
// force member collection to load -- at present this is necessary even though FetchType is EAGER
ca.getClassCs();
listClassA.add(ca);
}
// The collections of classCs are in are in memory here
for(ClassA a : listClassA){
log.info(a.getId() + "-" + a.getClassCs().size());
}
// Load data set 'B'
List<ClassB> listClassB = new ArrayList<ClassB>();
String sq = "from classB as b where ...";
Query q = Application.session.createQuery(sq);
listClassB.addAll(q.list());
// The collections of classCs are NOT in memory and touching collections does not force reload
// The collections exist but now all have size zero
for(ClassA a : listClassA){
log.info(a.getId() + "-" + a.getClassCs().size());
}
return;
}
I turns out that Hibernate can optionally flush the session cache before each query. One of the settings to control the session cache is session.setFlushMode(). In Hibernate 3.2.5, setting flush mode to:
FlushMode.COMMIT or FlushMode.MANUAL
achieves the desired result. I found this while reading "Hibernate in Action".
<!-- Clasa Proiect-->
<class catalog="tema3" name="com.tema3.tables.Proiect" table="proiect" lazy="false">
<id column="proiect_id" name="id">
<generator class="native"/>
</id>
<property name="nume" type="text" />
<set name="itemList" table="proiect_item" fetch="select">
<key column="proiect_id"/>
<many-to-many column="item_id" unique="true" class="com.tema3.tables.Item"/>
</set>
</class> </class>
that is my mapping class.
the code in wich i call an instantiation is :
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from Proiect");
List<Proiect> lists = HibernateUtil.listAndCast(q);
session.getTransaction().commit();
obj = lists;
and this is listandCastMethod:
public static <T> List<T> listAndCast(Query q) {
#SuppressWarnings("unchecked")
List list = q.list();
return list;
}
And i keep geting a :
May 2, 2011 4:38:03 PM org.hibernate.LazyInitializationException <init>
SEVERE: failed to lazily initialize a collection of role: com.tema3.tables.Proiect.itemList, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.tema3.tables.Proiect.itemList, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
but the thing is that the fields of Proiect class are populated except the Items set where i get this exception, but i want the items set to be populated.How can i do that?
Initialize the set of items inside the listAndCast(Query q) method before returning or calling a transaction.commit(). This can be done by calling any of the accessors on an element from the set of items. This will force hibernate to initialize the set of items rather than returning a proxy list.
EDIT
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from Proiect");
List<Project> lists = HibernateUtil.listAndCast(q);
if(lists != null && lists.size() > 0) {
Set<Item> s = lists.get(0).getItemList();
Iterator iter = s.iterator();
while(iter.hasNext()) {
Item item = iter.next();
item.getSomething();
break;
}
}
session.getTransaction().commit();
obj = lists;
I found the answer.It needs to add to the set declaration in the XML the property
lazy="false"