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 Oct 13 18:29:17 BST 2004 -->
<hibernate-mapping package="co.uk.fastest.foi.hibernate">
<class name="Users" table="users">
<id name="usersUsername" column="users_username" type="java.lang.String">
<generator class="assigned"/>
</id>
<property name="usersPassword" column="users_password" type="java.lang.String" not-null="true" />
<property name="usersLevel" column="users_level" type="java.lang.Integer" 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.Long" 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>
create table Users
(
users_username varchar(10) not null primary key,
users_password varchar(10) not null,
users_level int not null,
users_email varchar(254) not null,
users_department varchar(50) not null,
users_title varchar(35) not null,
users_job_title varchar(100),
users_first_name varchar(35) not null,
users_initials varchar(5) not null,
users_surname varchar(35) not null,
users_telephone bigint not null,
users_gen_password varchar(10) not null,
users_last_login date
);
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.
Hibernate: insert into users (users_password, users_level, users_email, users_department, users_title, users_job_title, users_first_name, users_initials, users_surname, users_telephone, users_gen_password, users_last_login, users_username) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
net.sf.hibernate.JDBCException: Could not execute JDBC batch update
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2410)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2360)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at Test.Test1.<init>(Test1.java:47)
at Test.Test1.main(Test1.java:86)
Caused by: Batch entry 0 insert into users (users_password, users_level, users_email, users_department, users_title, users_job_title, users_first_name, users_initials, users_surname, users_telephone, users_gen_password, users_last_login, users_username) values ( was aborted. Call getNextException() to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:126)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
... 6 more
Name and version of the database you are using:
PostgreSQL ( Redhats ) 7.3.6
The generated SQL (show_sql=true):
Hibernate: insert into users (users_password, users_level, users_email, users_department, users_title, users_job_title, users_first_name, users_initials, users_surname, users_telephone, users_gen_password, users_last_login, users_username) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Debug level Hibernate log excerpt:
Here is the code called
public static void addUsers ( Users users ) {
try {
// load a jdbc instance from the session factory
session = HibernateSessionFactory.currentSession ( );
session.save(users);
session.flush();
}
catch ( HibernateException e ) { // start of the catch hibernate exception
throw new RuntimeException ( e );
} // end of the hibernate exception
finally { // start of the finally statement
if ( session != null ) { // start of the if statement
try { // start of the try statement
session.close ( );
} // end of the try statement
catch ( HibernateException e ) { // start of the catch block
throw new RuntimeException ( e );
} // end of the catch statement
} // end of the if statement
} // end of the finally statement
}
And the test method
package Test;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import co.uk.fastest.foi.hibernate.Users;
import co.uk.fastest.foi.hibernate.Database;
import java.util.Date;
public class Test1 {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
public Test1 ( ) {
Users users = new Users ( );
users.setUsersDepartment("God");
users.setUsersEmail("tom@fastest.org.uk");
users.setUsersFirstName("Tom");
users.setUsersGenPassword("liverpool");
users.setUsersInitials("TJM");
users.setUsersJobTitle("God");
users.setUsersLastLogin(new Date());
users.setUsersLevel(new Integer(1));
users.setUsersPassword("liverpool");
users.setUsersSurname("Malone");
users.setUsersTelephone(new Long(016352547));
users.setUsersTitle("TJM");
users.setUsersUsername("tom");
Database.addUsers(users);
/* users = null;
users = Database.getUsers("tom");
System.out.print(users.getUsersEmail()); */
}
public static void main ( String [] args ) {
new Test1();
}
}
Sorry for keep asking silly questions but what have i done wrong.
Thanks in advance
Tom