# Insert-Problem mit JPA/Hibernate



## Mario__ (25. Feb 2009)

Hallo,

ich möchte Objekte über JPA/Hibernate in eine MYSQL Datenbank speichern, die Objekte und das Datenbankschema habe ich aus einer XSD über HyperJaxb3 generieren lassen. 

Ein Beispiel Objekt:

[HIGHLIGHT="Java"]
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-520 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2009.02.25 at 01:59:55 PM GMT+01:00 
//


package generated;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.jvnet.jaxb2_commons.lang.Equals;
import org.jvnet.jaxb2_commons.lang.HashCode;
import org.jvnet.jaxb2_commons.lang.builder.JAXBEqualsBuilder;
import org.jvnet.jaxb2_commons.lang.builder.JAXBHashCodeBuilder;



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "documentId"
})
@XmlRootElement(name = "application-reference")
@Entity(name = "generated.ApplicationReference")
@Table(name = "APPLICATIONREFERENCE")
@Inheritance(strategy = InheritanceType.JOINED)
public class ApplicationReference
    implements Equals, HashCode
{

    @XmlElement(name = "document-id", required = true)
    protected List<generated.DocumentId> documentId;
    @XmlAttribute
    @XmlSchemaType(name = "anySimpleType")
    protected String ucid;
    @XmlAttribute(name = "appl-type")
    @XmlSchemaType(name = "anySimpleType")
    protected String applType;
    @XmlAttribute(name = "us-series-code")
    @XmlSchemaType(name = "anySimpleType")
    protected String usSeriesCode;
    @XmlAttribute(name = "us-art-unit")
    @XmlSchemaType(name = "anySimpleType")
    protected String usArtUnit;
    @XmlAttribute(name = "is-representative")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String isRepresentative;
    @XmlAttribute
    @XmlSchemaType(name = "anySimpleType")
    protected String id;
    @XmlAttribute
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String status;
    @XmlAttribute(name = "Hjid")
    protected Long hjid;


    @OneToMany(targetEntity = generated.DocumentId.class, cascade = {
        CascadeType.ALL
    })
    @JoinColumn(name = "DOCUMENTID_APPLICATIONREFERE_0")
    public List<generated.DocumentId> getDocumentId() {
        if (documentId == null) {
            documentId = new ArrayList<generated.DocumentId>();
        }
        return this.documentId;
    }


    public void setDocumentId(List<generated.DocumentId> documentId) {
        this.documentId = documentId;
    }

    @Basic
    @Column(name = "UCID")
    public String getUcid() {
        return ucid;
    }


    public void setUcid(String value) {
        this.ucid = value;
    }

    @Basic
    @Column(name = "APPLTYPE")
    public String getApplType() {
        return applType;
    }

    public void setApplType(String value) {
        this.applType = value;
    }

    @Basic
    @Column(name = "USSERIESCODE")
    public String getUsSeriesCode() {
        return usSeriesCode;
    }

    public void setUsSeriesCode(String value) {
        this.usSeriesCode = value;
    }

    @Basic
    @Column(name = "USARTUNIT")
    public String getUsArtUnit() {
        return usArtUnit;
    }

    public void setUsArtUnit(String value) {
        this.usArtUnit = value;
    }

    @Basic
    @Column(name = "ISREPRESENTATIVE")
    public String getIsRepresentative() {
        if (isRepresentative == null) {
            return "NO";
        } else {
            return isRepresentative;
        }
    }

    public void setIsRepresentative(String value) {
        this.isRepresentative = value;
    }

    @Basic
    @Column(name = "ID")
    public String getId() {
        return id;
    }

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

    @Basic
    @Column(name = "STATUS")
    public String getStatus() {
        if (status == null) {
            return "new";
        } else {
            return status;
        }
    }

    public void setStatus(String value) {
        this.status = value;
    }

    @Id
    @Column(name = "HJID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getHjid() {
        return hjid;
    }

    public void setHjid(Long value) {
        this.hjid = value;
    }

}
[/HIGHLIGHT]

die persistent.xml ist:

[HIGHLIGHT="xml"]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" xmlns="http://java.sun.com/xml/ns/persistence" xmlnsrm="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <persistence-unit name="generated">
...
        <class>generated.ApplicationReference</class>
...
    </persistence-unit>
</persistence>

[/HIGHLIGHT]

hibernate properties:

```
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=sys
hibernate.connection.url=jdbc:mysql://localhost:3306/patent
hibernate.hbm2ddl.auto=create-drop
hibernate.show_sql=true
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
hibernate.jdbc.batch_size=0
```

Wenn ich jetzt ein Objekt speichern möchte erhalte ich vollgenden Fehler:

[HIGHLIGHT="Java"]
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert: [generated.ApplicationReference]
	at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
	at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:237)
	at org.jvnet.hyperjaxb3.ejb.test.RoundtripTest.checkSample(RoundtripTest.java:58)
	at org.jvnet.hyperjaxb3.ejb.test.AbstractEntityManagerSamplesTest$1.checkSample(AbstractEntityManagerSamplesTest.java:53)
	at org.jvnet.jaxb2_commons.test.AbstractSamplesTest.testSamples(AbstractSamplesTest.java:34)
	at org.jvnet.hyperjaxb3.ejb.test.AbstractEntityManagerSamplesTest.testSamples(AbstractEntityManagerSamplesTest.java:86)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at junit.framework.TestSuite.runTest(TestSuite.java:208)
	at junit.framework.TestSuite.run(TestSuite.java:203)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
	at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
	at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [generated.ApplicationReference]
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
	at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
	at org.hibernate.ejb.event.EJB3MergeEventListener.saveWithGeneratedId(EJB3MergeEventListener.java:43)
	at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:186)
	at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
	at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:687)
	at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:669)
	at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:245)
	at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
	at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
	at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
	at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
	at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:431)
	at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:178)
	at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
	at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:687)
	at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:669)
	at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:245)
	at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
	at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
	at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
	at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
	at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:431)
	at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:178)
	at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
	at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
	at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
	at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
	at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
	at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:228)
	... 30 more
Caused by: java.sql.SQLException: No value specified for parameter 8
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
	at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2179)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1996)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1940)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1925)
	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
	... 65 more
[/HIGHLIGHT]


und ich sehe das PreparedStatement von Hibernate ist:

[HIGHLIGHT="SQL"]
Hibernate: insert into APPLICATIONREFERENCE (APPLTYPE, ID, ISREPRESENTATIVE, STATUS, UCID, USARTUNIT, USSERIESCODE, HJID) values (?, ?, ?, ?, ?, ?, ?, ?)
[/HIGHLIGHT]

Was normal nicht sein dürfte, da in der Klasse @GeneratedValue(strategy = GenerationType.AUTO) vor HJID steht und das dann bei einem insert nicht berücksichtigt werden soll, anscheinend doch 

Sollte eigentlich so sein:
[HIGHLIGHT="SQL"]
Hibernate: insert into APPLICATIONREFERENCE (APPLTYPE, ID, ISREPRESENTATIVE, STATUS, UCID, USARTUNIT, USSERIESCODE) values (?, ?, ?, ?, ?, ?, ?)
[/HIGHLIGHT]



Die Fehlermeldung ist somit erklärt, aber was mach ich mit HJID? dieser ist ein autoincremented in MYSQL.

komisch war auch als ich mit hsqldb getestet habe hat es funktioniert. Dort macht es anscheinend nichts aus wenn insert statements teilweise leer sind 


hoffe jemand kann mir helfen

Hier noch die librarys die ich verwende:

activation-1.1.jar
annotate-0.2.GA.jar
annox-0.2.GA.jar
antlr-2.7.6.jar
asm-1.5.3.jar
asm-attrs-1.5.3.jar
basic-0.2.GA.jar
cglib-2.1_3.jar
commons-beanutils-1.7.0.jar
commons-collections-2.1.1.jar
commons-io-1.2.jar
commons-lang-2.2.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-3.2.4.ga.jar
hibernate-annotations-3.3.0.ga.jar
hibernate-commons-annotations-3.0.0.ga.jar
hibernate-entitymanager-3.3.1.ga.jar
hibernate-validator-3.0.0.ga.jar
hsqldb-1.8.0.7.jar
hyperjaxb3-ejb-plugin-0.3.jar
hyperjaxb3-ejb-roundtrip-0.3.jar
hyperjaxb3-ejb-runtime-0.3.jar
hyperjaxb3-ejb-schemas-customizations-0.3.jar
hyperjaxb3-ejb-schemas-persistence-0.3.jar
javassist-3.3.ga.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.6.jar
jaxb-xjc-2.1.6.jar
jboss-common-core-2.0.4.GA.jar
jsr173_api-1.0.jar
jta-1.0.1B.jar
junit-3.8.1.jar
lib.txt
log4j-1.2.13.jar
mysql-connector-java-5.1.5-bin.jar
persistence-api-1.0.jar
runtime-0.2.GA.jar
spring-2.0.2.jar
testing-0.2.GA.jar
tools-0.2.GA.jar
xmlunit-1.0.jar


lg Mario


----------



## Mario__ (26. Feb 2009)

Fehlerbehoben durch folgende libs:

activation-1.1.jar
annotate-0.2.GA.jar
annox-0.2.GA.jar
antlr-2.7.6.jar
asm-1.5.3.jar
asm-attrs-1.5.3.jar
basic-0.2.GA.jar
cglib-2.1_3.jar
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-io-1.2.jar
commons-lang-2.2.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate3.jar
hsqldb-1.8.0.7.jar
hyperjaxb3-ejb-plugin-0.3.jar
hyperjaxb3-ejb-roundtrip-0.3.jar
hyperjaxb3-ejb-runtime-0.3.jar
hyperjaxb3-ejb-schemas-customizations-0.3.jar
hyperjaxb3-ejb-schemas-persistence-0.3.jar
javassist-3.3.ga.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.6.jar
jaxb-xjc-2.1.6.jar
jboss-common-core-2.0.4.GA.jar
jsr173_api-1.0.jar
jta-1.1.jar
junit-3.8.1.jar
log4j-1.2.13.jar
mysql-connector-java-5.1.5-bin.jar
runtime-0.2.GA.jar
slf4j-api-1.5.6.jar
slf4j-log4j12-1.5.6.jar
spring-2.0.2.jar
testing-0.2.GA.jar
tools-0.2.GA.jar
xmlunit-1.0.jar


----------



## GilbertGrape (26. Feb 2009)

lol aha


----------



## jonasXYz (9. Apr 2010)

Hallo,

ich habe ein ähnliches Problem. Allerdings kann man an deinen geposteten Librarys teilweise nicht erkennen um welche Versionen es sich handelt. Welche Hibernate Version verwendest du letztendlich?

Grüße,
Jonas


----------



## SlaterB (9. Apr 2010)

optimistische Frage an jemanden mit nur 4 Posts, die alle über ein Jahr zurückliegen
(edit: allerdings: "Letzte Aktivität: 19.02.2010 08:39")


----------

