-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: generated id problem
PostPosted: Thu Nov 17, 2005 12:42 pm 
Newbie

Joined: Thu Sep 15, 2005 2:48 pm
Posts: 4
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/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 4:43 am 
Beginner
Beginner

Joined: Wed Jan 21, 2004 10:15 am
Posts: 24
Location: Munich, Germany
Maybe it works when you set the GeneratorType to 'identity' or 'native'. The latter will also work when you switch the RDBMS from MySQL to something else.

Christian


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 6:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
We can't really see cause you do not give us the exact version but my guess is that getGeneratedKey fails on you JDBC driver
try to set
hibernate.jdbc.use_get_generated_keys false

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.