aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate's @GeneratedValue(strategy = IDENTITY) isn't working :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate Watch "hibernate New topic
Author

hibernate's @GeneratedValue(strategy = IDENTITY) isn't working :(

Juan Carmona
Greenhorn

Joined: Apr 08, 2012
Posts: 3
hi anyone, I have a problem with the hibernate pk generation. I'm using MySQL 5.5 and Hibernate 4 and the config in my META-INF/persistence.xml is:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="manager1" transaction-type="JTA">
<class>Person</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="xxxx"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/xxxx"/>
<property name="hibernate.connection.username" value="xxxx"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
</properties>
</persistence-unit>
</persistence>

I'm not using orm.xml... and my POJO looks like this:

@Entity
@Table(name = "person", catalog = "xxxx", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Person implements java.io.Serializable {

public Person() {}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, columnDefinition = "BIGINT NOT NULL AUTO_INCREMENT")
public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

@Version
@Column(name = "version", nullable = false)
public long getVersion() {
return this.version;
}

public void setVersion(long version) {
this.version = version;
}

@Column(name = "name", unique = true, nullable = false)
public String getNombre() {
return this.nombre;
}

.....


I have a dao class that uses this pojo and I expose it as a webservice... but when I test the persist(Person p) method in soapUI, I realize that the @GeneratedValue(strategy = IDENTITY) isn't working.. I search the web and found the @Column(columnDefinition = "BIGINT NOT NULL AUTO_INCREMENT") but it doesn't work too

my soapUI test is...

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="http://impl.ejb.pape.jm.cl/">
<soapenv:Header/>
<soapenv:Body>
<impl:persist>
<arg0>
<id/>
<version/>
<name>aaa</name>
</arg0>
</impl:persist>
</soapenv:Body>
</soapenv:Envelope>


the jboss server responds:


Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[javax.xml.bind.UnmarshalException: For input string: ""
- with linked exception:
[java.lang.NumberFormatException: For input string: ""]]
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:434)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:371)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:348)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:778)
... 28 more

If I put a value to version, the problem is the same... and when I put a value to version and id (just to test).. the problem change to org.hibernate.PersistentObjectException: detached entity passed to persist ... but my idea is not to put the id..
anyway, do I have to put a value to version? Please help.. I'm new to hibernate and jpa .. maybe I'm I'm missing something

Juan
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

try @GeneratedValue(strategy = AUTO) I am not very sure whether MySQL Supports IDENTITY or not, but AUTO works .
Juan Carmona
Greenhorn

Joined: Apr 08, 2012
Posts: 3
thanks for the responde... but it doesn't work too

with @GeneratedValue(strategy = GenerationType.AUTO) I have the same exception

Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[javax.xml.bind.UnmarshalException: For input string: ""
- with linked exception:
[java.lang.NumberFormatException: For input string: ""]]
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:434)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:371)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:348)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:778)
... 28 more

any other idea? please help
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

My Bad, the issue is with de-serialization of your request. solution could be one of the following-
1) if you have your custom deserializer, then modify it to skip the id or eat up the exception.
2) remove the id node from request xml & always populate the version.

here, the always preferred thing is eating up of exception if empty values are passed for non mandatory values.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate's @GeneratedValue(strategy = IDENTITY) isn't working :(
 
Similar Threads
Problem whne used custom squence name in JPA (hibernate) annotations
JPA/Hibernate: @GeneratedValue doesnt work
Identity issue for EJB3.0 Entity along with Hibernate HBM configuration
Hibernate + Dynamic Groovy Class (Is this even possible)
Foreign key value null if nullable true in @OneToMany assoc