Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie Question--Hibernate will not persist

 
Russell Ray
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say post your mapping and your code that ends up calling this part of Hibernate that is throwing that exception.

Mark
 
Russell Ray
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic