Hibernate version:
2.1.4
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Sep 01 14:55:17 BST 2004 -->
<hibernate-mapping package="co.uk.fastest.foi.hibernate">
<class name="Users" table="Users">
<id name="usersId" column="users_id" type="java.lang.String">
<generator class="native"/>
</id>
<property name="usersUsername" column="users_username" type="java.lang.String" not-null="true" />
<property name="usersPassword" column="users_password" type="java.lang.String" not-null="true" />
<property name="usersLevel" column="users_level" type="java.lang.String" not-null="true" />
<property name="usersEmail" column="users_email" type="java.lang.String" not-null="true" />
<property name="usersDepartment" column="users_department" type="java.lang.String" not-null="true" />
<property name="usersTitle" column="users_title" type="java.lang.String" not-null="true" />
<property name="usersJobTitle" column="users_job_title" type="java.lang.String" />
<property name="usersFirstName" column="users_first_name" type="java.lang.String" not-null="true" />
<property name="usersInitials" column="users_initials" type="java.lang.String" not-null="true" />
<property name="usersSurname" column="users_surname" type="java.lang.String" not-null="true" />
<property name="usersTelephone" column="users_telephone" type="java.lang.String" not-null="true" />
<property name="usersGenPassword" column="users_gen_password" type="java.lang.String" not-null="true" />
<property name="usersLastLogin" column="users_last_login" type="java.util.Date" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
package co.uk.fastest.foi.hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link
http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static net.sf.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
}
Full stack trace of any exception that occurs:
log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.RuntimeException: net.sf.hibernate.HibernateException: The database returned no natively generated identity value
at co.uk.fastest.foi.hibernate.Database.addUsers(Database.java:162)
at Application.<init>(Application.java:36)
at Application.main(Application.java:48)
Caused by: net.sf.hibernate.HibernateException: The database returned no natively generated identity value
at net.sf.hibernate.persister.AbstractEntityPersister.getGeneratedIdentity(AbstractEntityPersister.java:1223)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:528)
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:925)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:850)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:768)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:731)
at co.uk.fastest.foi.hibernate.Database.addUsers(Database.java:155)
... 2 more
Name and version of the database you are using:
MySQL 4.0.20
Debug level Hibernate log excerpt:
I'm all confused, i have a simple Users table, with a user id , user name etc. I can search it for login etc. I have problems adding to the db.
I add a random Users instance to the db, and i get the following error, but it does add it to the db. And if you then do the same program again, it doesn't add a new row with a new id get a key error included below.
log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.RuntimeException: net.sf.hibernate.JDBCException: could not insert: [co.uk.fastest.foi.hibernate.Users]
at co.uk.fastest.foi.hibernate.Database.addUsers(Database.java:162)
at Application.<init>(Application.java:36)
at Application.main(Application.java:48)
Caused by: net.sf.hibernate.JDBCException: could not insert: [co.uk.fastest.foi.hibernate.Users]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:556)
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:925)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:850)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:768)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:731)
at co.uk.fastest.foi.hibernate.Database.addUsers(Database.java:155)
... 2 more
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 net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:526)
... 9 more
I am fairly sure that the problem lies in my choice of id but i'm all confused any help would be most appreciated.
Thanks in advance Tom