When I try to persist an object I get an exception indicating that the database returned no natively generated identity value. Any help would be appreciated. See details below...
Hibernate version: 3.1
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?> <entity-manager> <name>MyPersistenceContext</name> <class>Person</class> <properties> <property name="hibernate.connection.url" value="jdbc:mysql://localhost/demodb"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.password" value="secret"/> <property name="hibernate.connection.username" value="demo"/> </properties> </entity-manager>
Code:
EntityManagerFactory entityManagerFactory = Persistence .createEntityManagerFactory("MyPersistenceContext"); EntityManager entityManager = entityManagerFactory .createEntityManager(); EntityTransaction tx = entityManager.getTransaction(); tx.begin(); Person person = new Person("Andretti", "Mario"); entityManager.persist(person); tx.commit(); entityManager.close();
Person Code:
import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratorType; import javax.persistence.Id;
@Entity public class Person {
private Long id;
private String lastName;
private String firstName;
public Person() { }
public Person(String lastName, String firstName) { // TODO Auto-generated constructor stub this.lastName = lastName; this.firstName = firstName; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
@Basic(optional=false) @Id(generate=GeneratorType.AUTO) @Column(nullable=true) public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
}
Full stack trace:
log4j:WARN No appenders could be found for logger (org.hibernate.ejb.HibernatePersistence). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.HibernateException: The database returned no natively generated identity value at org.hibernate.id.IdentifierGeneratorFactory.getGeneratedIdentity(IdentifierGeneratorFactory.java:33) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1820) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2253) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:266) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:164) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101) at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:124) at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:84) at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:530) at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:130) at SavePerson.main(SavePerson.java:16)
Name and version of the database: MySQL 4.1
_________________ Jeff Brown
brown_j@ociweb.com
Principal Software Engineer
Object Computing Inc.
http://www.ociweb.com/
Autism Strikes 1 in 250
Find The Cause ~ Find The Cure
http://www.jeffandbetsy.net/walkfar2005/
http://www.naar.org/
|