In my Collection.hbm.xml file (I am using my own collection class). I believe I am getting an error in this file.
Here is my Collection.hbm.xml file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Collection" table="COLLECTION">
<id name="collectionID" type="int" unsaved-value="0">
<column name="COLLECTIONID" sql-type="int" not-null="true"/>
<generator class="identity" />
</id>
<property name="name"/>
<property name="targetID"/>
<property name="parentID"/>
</class>
</hibernate-mapping>
Here is my CollectionServer code that involves Hibernate code:
try {
out.println("Begin Session1.<br/>");
Session session = HibernateUtil.currentSession();
out.println("Session1 Established.<br/>");
/*
Configuration cfg = new Configuration()
.addFile("Collection.hbm.xml");
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession(); // open a new Session
*/
Transaction tx= session.beginTransaction();
out.println("Transaction1 begun.");
Collection princess = new Collection();
princess.setName("CollectionNumber1");
princess.setTargetID(new Integer(2));
princess.setParentID(new Integer(3));
session.save(princess);
tx.commit();
HibernateUtil.closeSession();
out.println("CollectionYo");
}
catch (HibernateException he)
{
out.println("HibernateException: " + he.getMessage());
he.printStackTrace(out);
}
catch (NestableException ne)
{
out.println("NestableException: " + ne.getMessage());
ne.printStackTrace(out);
}
catch (Exception e)
{
out.println("Exception: " + e.getMessage());
e.printStackTrace(out);
}
out.println("</BODY></HTML>");
}
Here is my hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/quickstart</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="Collection.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Here is my POJO :
// Collection.java
import java.io.*;
public class Collection {
private int collectionID;
private String name;
private Integer targetID;
private Integer parentID;
public Collection() {
}
public int getCollectionID() {
return collectionID;
}
public void setCollectionID(int collectionID) {
this.collectionID = collectionID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getTargetID() {
return targetID;
}
public void setTargetID(Integer targetID) {
this.targetID = targetID;
}
public Integer getParentID() {
return parentID;
}
public void setParentID(Integer parentID) {
this.parentID = parentID;
}
}
This is my error message:
HibernateException: could not insert: [Collection]
net.sf.hibernate.JDBCException: could not insert: [Collection]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:558)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at CollectionServlet.doGet(CollectionServlet.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:419)
at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:793)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:702)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:571)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:644)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Duplicate key or integrity constraint violation, message from server: "Duplicate entry '0' for key 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1977)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2236)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1741)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1588)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:233)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:528)
... 37 more
I believe it is a problem with my <generator class="identity" /> . If I use uuid.hex and change my collectionID type to String, it works because I think it uses my IP address. Any help would be appreciated.
By the way I read your guy's post of stopping to provide free posts of hibernate. I greatly appreciate your guy's help. In a lot of forums I don't get the instant response that I get from the Hibernate Team. So I hope you guys pursue giving help.
Thx,
Mush
|