Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
[b]Hibernate version:[/b]
3.0
[b]Mapping documents:[/b]
SubscriberMaster.hbm.xml
**********************
<typedef name="encryptedString" class="org.jasypt.hibernate.type.EncryptedStringType">
<param name="encryptorRegisteredName">strongHibernateStringEncryptor</param>
</typedef>
<class name="roseindia.tutorial.hibernate.SubscriberMaster" table="subscriber_master">
<id name="msisdn" type="encryptedString">
<column name="MSISDN" length="50" />
<generator class="assigned" />
</id>
<many-to-one name="langTypes" class="roseindia.tutorial.hibernate.LangTypes" fetch="select">
<column name="LANG_TYPE" length="2" />
</many-to-one>
<property name="name" type="encryptedString">
<column name="NAME" length="50" />
</property>
<property name="password" type="encryptedString">
<column name="PASSWORD" length="50" />
</property>
<set name="childMasters" inverse="true">
<key>
<column name="PARENT_MSISDN" length="50" not-null="true" />
</key>
<one-to-many class="roseindia.tutorial.hibernate.ChildMaster" />
</set>
</class>
ChildMaster.hbm.xml
*****************
<class name="roseindia.tutorial.hibernate.ChildMaster" table="child_master">
<id name="childMsisdn" type="string">
<column name="CHILD_MSISDN" length="50" />
<generator class="assigned" />
</id>
<many-to-one name="subscriberMaster" class="roseindia.tutorial.hibernate.SubscriberMaster" fetch="select">
<column name="PARENT_MSISDN" length="50" not-null="true" />
</many-to-one>
<property name="childName" type="string">
<column name="CHILD_NAME" length="50" />
</property>
</class>
[b]Code between sessionFactory.openSession() and session.close():[/b]
List subscriber = session.createSQLQuery("select {subscriber.*} from SUBSCRIBER_MASTER subscriber")
.addEntity("subscriber", SubscriberMaster.class).list();
SubscriberMaster subscriberObject=null;
for (Iterator it = subscriber.iterator(); it.hasNext();) {
subscriberObject = (SubscriberMaster) it.next();
System.out.println("MSISDN: " + subscriberObject.getMsisdn());
System.out.println("name: " + subscriberObject.getName());
System.out.println("password: " + subscriberObject.getPassword());
System.out.println("LangTypes: " + subscriberObject.getLangTypes().getLangType());
break;
}
Transaction tx = session.beginTransaction();
ChildMaster child=new ChildMaster();
child.setChildMsisdn("9886179192");
child.setChildName("Vedha");
child.setSubscriberMaster(subscriberObject);
session.save(child);
tx.commit();
[b]Full stack trace of any exception that occurs:[/b]
Hibernate: select subscriber.MSISDN as MSISDN0_, subscriber.LANG_TYPE as LANG2_0_0_, subscriber.NAME as NAME0_0_, subscriber.PASSWORD as PASSWORD0_0_ from SUBSCRIBER_MASTER subscriber
MSISDN: 9886094359
name: Ram
password: Good1234
LangTypes: En
Hibernate: insert into child_master (PARENT_MSISDN, CHILD_NAME, CHILD_MSISDN) values (?, ?, ?)
Could not execute JDBC batch update
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:161)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at roseindia.tutorial.hibernate.TestRelations.main(TestRelations.java:84)
Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails (`test/child_master`, CONSTRAINT `FK2A8C7AE52B99E11F` FOREIGN KEY (`PARENT_MSISDN`) REFERENCES `subscriber_master` (`MSISDN`))"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1492)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
... 8 more
[b]Name and version of the database you are using:[/b]
MySQL 5
[b]The generated SQL (show_sql=true):[/b]
insert into child_master (PARENT_MSISDN, CHILD_NAME, CHILD_MSISDN) values (?, ?, ?)
[b]Debug level Hibernate log excerpt:[/b]
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html