aspose file tools*
The moose likes Object Relational Mapping and the fly likes Newbie Question--Hibernate will not persist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Newbie Question--Hibernate will not persist" Watch "Newbie Question--Hibernate will not persist" New topic
Author

Newbie Question--Hibernate will not persist

Russell Ray
Ranch Hand

Joined: Apr 25, 2005
Posts: 116
Okay, first let me apologize for such a basic question.....but I am so new I am at a lost on how to fix this simple issue.....

What I have done is created a MYSQL database with all the relationships. There are 3 Xref tables (Many to Many). One table uses a composite for the primary key. All the other tables are very simple relationships. I have used MyEclipse to reverse engineer the source code for Hibernate and Spring (DAOs). I corrected all the Spring errors and now I am onto the Hibernate error. I have updated the hbm.xml file adding the cascade styles for each class and changed the generator for each primary key to native. That's all the changes for the hbm.xl files

I made the first trial as simple as possible by only persisting one object with NO relationships (empty HashSet). However, I am getting this error:



When I go looking at the Hibernate source code, I see this information concerning Hibernate's OnLockVisitor





As you can see for yourself this situation occurs if your collection is not a PersistentCollection (i.e. if you have replaced the collection completely after obtaining the object from the DB) or is you use array (not very likely). Because I have not replaced or created any collections, I am at a lost on how this could be...


Any suggestions would be greatly appreciated.

Russ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

I'd say post your mapping and your code that ends up calling this part of Hibernate that is throwing that exception.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Russell Ray
Ranch Hand

Joined: Apr 25, 2005
Posts: 116
Mark,

Thank you so much for reading my post..........

Here is my mapping file:





Here the method that calls the Hibernate framework:



As you can see most of this was generated for me. I have made very little changes.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

So you are just setting a lock on an object? and it is just that line?

getHibernateTemplate().lock(instance, LockMode.NONE);

What do you need the lock for?

I must apologize, I do not use Spring, so I am not familiar with their framework.

for persist I usually have code like the following

Session session = sessionFactory.getSession();
Transaction t = session.beginTransaction();
MyObject m = new MyObject(blah blah blah, set up data);
session.saveOrUpdate(m);
t.commit()

All within a try-catch-finally where I clean up. Now with JPA the code is a little simpler with the EntityManager being injected, and if I was using EJB3 Stateless Sessio bean then I would just have

SOme transaction annotation then


Not that that really helps.

Mark
Russell Ray
Ranch Hand

Joined: Apr 25, 2005
Posts: 116
Mark,

This is just the information I need.......

The tool I am using (MyEclipse) generates all the code for the DAOs. The DocumentDAO extends the HibernateSupportDAO. It handles most of the required method generation.

From your comment I can tell you are using the JDBC Template. I changed my code to use something very similar. The SQL generated call for UPDATE of the table. It return zero because no record was in the database with the id of 1. Actually the table is empty. When I tried the save function the SQL generated was an INSERT statement. In both cases no data was stored in the database.

The lock method obtains the specific lock level upon the given object, implicitly checking whether the corresponding database entry still exist. In my case, the lock mode is none. The database is empty so there is no corresponding database entry.

As far as JPA is concerned......It's a hard locked into Spring and Hibernate.

Russ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

From your comment I can tell you are using the JDBC Template.


Nope, I don't use any template. I don't use Spring.

Mark
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Newbie Question--Hibernate will not persist