Quote:
when i use the primary key of one table as a foreign key for another i have no problems but when i use a unique key i get an error... is it possible for me to use unique key as my foreign key in another table the following code is auto generated in eclipse with some minor changes like <cascade="all",<generaor ="increment"> etc.. is there some changes tat i hav to make for it to work
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 24, 2006 10:21:04 PM by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
<class name="com.EsaaGroup.Grouptable" table="grouptable" catalog="test">
<comment></comment>
<id name="id" type="int">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="string">
<column name="name" length="45" not-null="true" unique="true">
<comment></comment>
</column>
</property>
<set name="stories" cascade="all" inverse="true">
<key>
<column name="info" length="45" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="com.EsaaGroup.Story" />
</set>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 24, 2006 10:21:04 PM by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
<class name="com.EsaaGroup.Story" table="story" catalog="test">
<comment></comment>
<id name="id" type="int">
<column name="id" />
<generator class="increment" />
</id>
<many-to-one name="grouptable" class="com.EsaaGroup.Grouptable" fetch="select">
<column name="info" length="45" not-null="true">
<comment></comment>
</column>
</many-to-one>
<property name="parentId" type="int">
<column name="parent_id" not-null="true">
<comment></comment>
</column>
</property>
</class>
</hibernate-mapping>
Code:
import java.io.Serializable;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.criterion.*;
import org.hibernate.event.*;
import org.hibernate.event.def.*;
import com.EsaaGroup.Grouptable;
import com.EsaaGroup.Story;
public class EsaaGroup {
public static void main(String[] args) throws Exception {
// HibernateUtil.setup("create table grouptable (id int,name varchar);");
// HibernateUtil.setup("create table story (id int,info varchar,idx int,parent_id int);");
SessionFactory sf;
sf=new Configuration().configure("hibernateGroup.cfg.xml").buildSessionFactory();
System.out.println("Session Factorty created");
Session session=sf.openSession();
System.out.println("session created");
Grouptable sp = new Grouptable();
sp.setName("Esaa Gp11");
//
// Grouptable sp = (Grouptable) session.load(Grouptable.class, new Integer(2));
// sp.setName("New Esaa Test");
Story s = new Story();
// s.setInfo("New Esaa Strory11-1");
s.setParentId(111);
s.setGrouptable(sp);
sp.getStories().add(s);
Story s1 = new Story();
// s1.setInfo("New Esaa Strory11-2");
s1.setParentId(112);
s1.setGrouptable(sp);
sp.getStories().add(s1);
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(sp);
// session.save(s);
transaction.commit();
System.out.println("Transaction commited");
} catch (Exception e) {
System.out.println("Exception Here");
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
session.close();
}
// HibernateUtil.checkData("select * from grouptable");
// HibernateUtil.checkData("select * from story");
}
}
[/list]
IWAV0055I Java Bean EsaaGroup started with the main method
Session Factorty created
session created
Hibernate: select max(id) from grouptable
Hibernate: select max(id) from story
Hibernate: insert into test.grouptable (name, id) values (?, ?)
Hibernate: insert into test.story (info, parent_id, id) values (?, ?, ?)
Hibernate: insert into test.story (info, parent_id, id) values (?, ?, ?)
Exception Here
IWAV0052E Invocation Target Exception creating EsaaGroup
java.lang.reflect.InvocationTargetException
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 org.eclipse.ve.internal.java.vce.launcher.remotevm.JavaBeansLauncher.main(JavaBeansLauncher.java:79)
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
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:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at EsaaGroup.main(EsaaGroup.java:51)
... 5 more
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/story`, CONSTRAINT `FK_story_1` FOREIGN KEY (`info`) REFERENCES `grouptable` (`name`))"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1492)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 13 more