-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Confuse with hibernate behaviour
PostPosted: Thu Dec 07, 2006 4:54 pm 
Newbie

Joined: Thu Dec 07, 2006 4:30 pm
Posts: 3
I made my DAOTest extends AbstractTransactionalDataSourceSpringContextTests. and try to persist 2 object with same unique key.

Quote:
<hibernate-mapping>
<class name="sample.domain.School" table="SCHOOL">
<id name="id" column="SCHOOL_ID" length="32">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="registerNumber" unique="true" not-null="true"/>
</class>
</hibernate-mapping>


However, the exception does not throw after I invoked the
Quote:
saveOrUpdate(school1)
saveOrUpdate(school2)


but if I flush the session or excute a query, the exception will be thrown out. Does it mean hibernate will not check the constraint when the instance is only saved to session, that job actually left to database?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 12:04 am 
Newbie

Joined: Thu Dec 07, 2006 4:30 pm
Posts: 3
Following is my code.

I checked this page : Hibernate violate the unique key constraint, I flush the session before the second one commit, still got the problem.

Can anyone give me a hint about this?



http://www.hibernate.org/117.html#A8



Quote:
package sample;

import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

import sample.dao.SchoolDAO;
import sample.domain.School;

public class SchoolDAOTest extends AbstractTransactionalDataSourceSpringContextTests {
private SchoolDAO schoolDAO;
private School school;
@Override
protected String[] getConfigLocations() {
return new String[]{"classpath*:context/*.xml"};
}

@Override
protected void onSetUpBeforeTransaction() throws Exception {
super.onSetUpBeforeTransaction();
schoolDAO = (SchoolDAO) this.applicationContext.getBean("schoolDAO");
school = new School();
school.setRegisterNumber("1");
}

public void testExceptionShouldBeThrownWhenUserTypeSameRegistrationKey() {
School school2 = new School();
schoolDAO.saveOrUpdate(school);
schoolDAO.getSessionFactory().getCurrentSession().flush();
school2.setRegisterNumber("1");
schoolDAO.saveOrUpdate(school2);
}
}
[url][/url]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 4:54 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Yes, this job (checking constraints) is left to database. Hibernate uses "write-behind" approch, that's why you recive an exception only after:
1. Query execution (Hibernate needs guarantee that objects, you've modified in session consistent with query results)
2. Commiting transaction
3. Explicit call Session.flush()

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 8:38 am 
Newbie

Joined: Thu Dec 07, 2006 4:30 pm
Posts: 3
thx


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.