-->
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.  [ 6 posts ] 
Author Message
 Post subject: ConstraintViolationException problem at db insert
PostPosted: Tue Jan 17, 2006 5:54 am 
Newbie

Joined: Wed Dec 14, 2005 11:36 am
Posts: 3
hi,

i wanna insert an object (Car in example) whose foreign key object (Company in example) may not exist. But i don't know how i should design mapping document. In other words Company may not exists, but i want to save cars into db.

Hibernate version: 3.1

Mapping documents:
Code:
Company.hbm
...
<bag name="carList" cascade="none" inverse="false" lazy="true">
            <key column="`nCompanyId`"/>
            <one-to-many class="Car"/>
</bag>
...
Car.hbm
...
<many-to-one name="company" column="`nCompanyId`"/>
...

Code between sessionFactory.openSession() and session.close():
Code:
t = session.beginTransaction();
session.save(car);
t.commit();

Full stack trace of any exception that occurs:
org.hibernate.exception.ConstraintViolationException: could not insert: [object.Car]
....
Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'nCompanyId', table 'mydb.dbo.Car'; column does not allow nulls. INSERT fails.
.....
Name and version of the database you are using: MS SQL Server 2000 SP4

The generated SQL (show_sql=true):
Code:
insert into Car ( [nCompanyId], [dAmount], [sDetail] ) values (?, ?, ?)
// values are (0, 0.0, 'anything') respectively


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 8:04 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
If Company is a "lookup" type of table, then I think the mapping is incorrect.

Do you have a separate mapping for Car, or does it only exist w/ in the context of a company?

Try making Car its own entity, and using many:1 to company- the lookup.

James


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 8:25 am 
Newbie

Joined: Wed Dec 14, 2005 11:36 am
Posts: 3
If Company is a "lookup" type of table, then I think the mapping is incorrect.
i can't understand meaning of "lookup". i also think the mapping is incorrect, but i dont know how i can correct.

Do you have a separate mapping for Car, or does it only exist w/ in the context of a company?
i have seperate mapping file (hbm) for both: Company and Car. In previous mail i have sent relationship between these tables. i need this realtionship becouse i get list of "car left outer join car.company".

Try making Car its own entity, and using many:1 to company- the lookup.
explain with example please


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 11:57 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
how did you create the database schema. i think hibernate is doing the right thing by trying to insert null for company column. Is this column nullable in the database.

Also you can try using

Car.hbm
...
<many-to-one name="company" column="`nCompanyId`" not-null="false"/>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 3:30 am 
Newbie

Joined: Wed Dec 14, 2005 11:36 am
Posts: 3
i don't use any tool to create my schemas.

company table:
Code:
nCompanyId          sName         ....
1                           Alfa             ....
2                           Beta            ....

car table
Code:
nCarId      nCompanyId      dAmount      ....
1              1                       0.0              ....
2              1                       0.0              ....
3              2                       0.0              ....


i can insert all rows above using my schema and hibernate, but i cant insert row bellow, because company whose nCompanyId is '0' does not exist. And i want to insert row bellow but how?

Code:
[b]car table[/b]
nCarId      nCompanyId      dAmount      ....
4              0                       0.0              ....



OK, i have added not-null param and altered table respective column null allowed

New Error Message
Code:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before
flushing: object.Company
.......


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 1:07 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
when car cannot exist without your company than must save your company first.
comp = new Comp();
car = new Car();
car.setComp(comp);
session.save(company);
// ad you defined cascade="none" you must also save car explicitly.
session.save(car);


...
if comp exist do
comp = (Comp) session.get(Comp.class, id);
car.setComp(comp);
session.save(car);

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.