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.1The 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]