-->
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: problem with Id generation in Hibernate
PostPosted: Sat Jul 29, 2006 6:17 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

--
Hi all
I am using Spring framework and hibernate.
I have a table as master and another as detail. those tables have a relationship:
--
Code:

@Entity(name = "Province")
@Table(name = "province")
public class Province extends EntityPayvand {
   private Integer id;

   private Country country;

   private Integer idCountry;

   private List<ProvinceLocale> provinceLocales;

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   @OneToMany(mappedBy="province",cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},fetch=FetchType.EAGER)
   public List<ProvinceLocale> getProvinceLocales() {
      return provinceLocales;
   }

   public void setProvinceLocales(List<ProvinceLocale> provinceLocales) {
      this.provinceLocales = provinceLocales;
   }


and the detail table:

Code:

@Entity(name="ProvinceLocale")
@Table(name="provincelocale")
@IdClass(ProvinceLocalePK.class)
public class ProvinceLocale implements Serializable{
   private Integer idProvince;
   private LocaleSelector id;
   private String name;
   private Province province;
   
   public ProvinceLocale() {
   }
   
   public ProvinceLocale(LocaleSelector selector, String name) {
      this.id = selector;
      this.name = name;
   }

   @ManyToOne
   @JoinColumn(name="id_province",insertable=false,updatable=false)
   public Province getProvince() {
      return province;
   }

   public void setProvince(Province province) {
      this.province = province;
   }

   @Id
   @Type(type="LocaleSelector")
   public LocaleSelector getId() {
      return id;
   }
   public void setId(LocaleSelector id) {
      this.id = id;
   }
   
   @Id
   public Integer getIdProvince() {
      return idProvince;
   }
   public void setIdProvince(Integer idProvince) {
      this.idProvince = idProvince;
   }
   
   @Column(name = "name",length = 100)
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }



the problem is, it is not going to generate an incremented Id for the master table or maybe it works fine but it can't set the assigned Id for detail table (the ProvinceLocale class defined as a List in Province)

please help me out and let me know if I made anything wrong.




Hibernate version: 3.1 10beta

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
(util.JDBCExceptionReporter 71 ) SQL Error: 1048, SQLState: 23000
(util.JDBCExceptionReporter 72 ) null, message from server: "Column 'id_province' cannot be null"
(def.AbstractFlushingEventListener 300 ) Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:394)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
at org.springframework.orm.hibernate3.HibernateTemplate.persist(HibernateTemplate.java:730)
at com.payvand.hl7.model.dao.hibernate.ProvinceDAOImpl.insert(ProvinceDAOImpl.java:55)
at com.payvand.hl7.model.dao.ProvinceDAOTest.testProviceLocale(ProvinceDAOTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: null, message from server: "Column 'id_province' cannot be null"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1404)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 25 more
(dao.ProvinceDAO 58 ) ####dao-Provincedao-insertProvince:Hibernate operation: Could not execute JDBC batch update; SQL [insert into provincelocale (name, id, id_province) values (?, ?, ?)]; null, message from server: "Column 'id_province' cannot be null"; nested exception is java.sql.BatchUpdateException: null, message from server: "Column 'id_province' cannot be null"####


Name and version of the database you are using: MySql 4.1

The generated SQL (show_sql=true):
Hibernate: insert into province (id_country) values (?)
Hibernate: insert into provincelocale (name, id, id_province) values (?, ?, ?)
Hibernate: insert into provincelocale (name, id, id_province) values (?, ?, ?)




regards
Mohammad[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 30, 2006 5:23 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Why this second @Id?

Code:
@Id
   public Integer getIdProvince() {
      return idProvince;
   }
   public void setIdProvince(Integer idProvince) {
      this.idProvince = idProvince;
   }   


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 30, 2006 5:44 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
alesj wrote:
Why this second @Id?

Code:
@Id
   public Integer getIdProvince() {
      return idProvince;
   }
   public void setIdProvince(Integer idProvince) {
      this.idProvince = idProvince;
   }   



the table has two primary key. I mean both of them are unique and primary key. idProvince is actually a foregn key from "Province".

table province (id, country_code)

table province_locale(id, id_province, name)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 30, 2006 5:57 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
>> the table has two primary key
I don't think that's the way you do composite ids.

But you already have province_id as foreign key mapped:
Code:
@ManyToOne
   @JoinColumn(name="id_province",insertable=false,updatable=false)
   public Province getProvince() {
      return province;
   }

   public void setProvince(Province province) {
      this.province = province;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 30, 2006 6:20 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
alesj wrote:
>> the table has two primary key
I don't think that's the way you do composite ids.

But you already have province_id as foreign key mapped:
Code:
@ManyToOne
   @JoinColumn(name="id_province",insertable=false,updatable=false)
   public Province getProvince() {
      return province;
   }

   public void setProvince(Province province) {
      this.province = province;
   }


---------

No, it is necessary, because in detail table the composition of id and id_province should be unique.

Code:
-----------------------
id        country_id  |
-----------------------
01        001           |
02        002           |
-----------------------


-----------------------------------------
province_id         id             name |
-----------------------------------------
01                     en             fars    |
01                     ar              ????   |
01                     it               .....    |
02                     en             gilan   |
02                     ar              ????   |
02                     it               ......    |
------------------------------------------

???? = in arabic language
..... = in italian language



Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 3:04 pm 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
It's much easier to map a composite PK with Embeddable/EmbeddedId:
Map province with @ManyToOne in the Embeddable class along with the other id property. If you need an inverse mapping for province, map it again in the main class with @ManyToOne and updatable = false, insertable = false. This works very neatly for me.

_________________
Henrique Sousa
Don't forget to rate useful responses


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.