openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scherer, Annette" <Annette.Sche...@huk-coburg.de>
Subject Re: Persist pojo
Date Fri, 09 May 2008 14:43:29 GMT
The @ForeignKey annotation is the solution.
I suppose that it causes the inserts to be made in the correct order, so that the objects
referred to by the foreign key are already in datebase, when needed.

Thank you for your support.


Greetings
Annette Scherer



-----Urspr√ľngliche Nachricht-----
Von: Michael Dick [mailto:michael.d.dick@gmail.com] 
Gesendet: Donnerstag, 8. Mai 2008 16:14
An: users@openjpa.apache.org
Betreff: Re: Persist pojo

Based on that I'd guess that Annette is connecting to a pre-existing
database, which has foreign key constraints that OpenJPA isn't aware of.

By default OpenJPA doesn't assume that there are foreign key constraints in
place for relationships. You can indicate where you have foreign key
constraints in place by adding the @ForeignKey annotation to your entities.
Ex :
import org.apache.openjpa.persistence.jdbc.ForeignKey;

public class MyEntity {
    @OneToOne
    @ForeignKey
    private AnotherEntity;
    . . .
}

OpenJPA can also be configured to read in ForeignKey information from the
database. To use this option add the following property to persistence.xml :

<property name="openjpa.jdbc.SchemaFactory"
value="native(ForeignKeys=true)"/>

Once OpenJPA is aware of the foreign key constraints then it will order the
updates accordingly.

-Mike


On Thu, May 8, 2008 at 9:05 AM, Landers, Richard <Richard.Landers@ct.gov>
wrote:

> DB2 error codes are documented here:
>
> http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp
>
> Type "sql0530" into the search box to search for documentation of an
> "SQLCODE: -530" error message.  Here's an excerpt:
>
>        SQL0530N
>        The insert or update value of the FOREIGN KEY constraint-name
>        is not equal to any value of the parent key of the parent table.
>
> --Rich
>
> -----Original Message-----
> From: Michael Vorburger [mailto:mvorburger@odyssey-group.com]
> Sent: Thursday, May 08, 2008 9:59 AM
> To: users@openjpa.apache.org
> Subject: RE: Persist pojo
>
> How about you check the DB2 documentation about what SQLCODE: -530,
> SQLSTATE: 23503 means?
>
> Sometimes it helps to try on another DB, e.g. an embedded Derby, for
> clarity.
>
>
> -----Original Message-----
> From: Scherer, Annette [mailto:Annette.Scherer@huk-coburg.de]
> Sent: jeudi, 8. mai 2008 15:53
> To: users@openjpa.apache.org
> Subject: Persist pojo
>
> Hi,
>
> I want to persist a pojo to Database per JPA. It fails with following
> message
> <openjpa-1.0.2-r420667:627158 nonfatal general error>
> org.apache.openjpa.persistence.PersistenceException: DB2 SQL error:
> SQLCODE: -530, SQLSTATE: 23503
>
> Thank you for valuable Information
>
>
> Here is the relevant coding:
>
> Annotated Classes:
>
> public class DomainAssociation implements Serializable {
>
>        @Column(name="DA_NAME", length=50)
>        @Id private String name;
>
>        @Column(name="DA_BEMERKUNG", length=200)
>        private String comment;
>
>        @OneToMany(mappedBy="domainAssociation", cascade=CascadeType.ALL
> , fetch=FetchType.EAGER)
>        private List<Domain> domains;
>
>
> public class Domain implements Serializable {
>
>        @Column(name="DOMAIN_NAME", length=50)
>        @Id private String name;
>
>        @Column(name="DOMAIN_VERWENDUNG", length=5)
>        private String use;
>
>        @Column(name="DOMAIN_TYP", length=10)
>        private String type;
>
>        @Column(name="DOMAIN_BEMERKUNG", length=200)
>        private String comment;
>
>        @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST)
>        @JoinColumn(name="DA_NAME")
>        private DomainAssociation domainAssociation;
>
>        @OneToMany(mappedBy="domain", cascade=CascadeType.ALL ,
> fetch=FetchType.EAGER)
>        private List<DomainValue> domainValues;
>
>
>
> Persistence.xml
>
> <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_1_0.xsd"
>        version="1.0">
>        <persistence-unit name="InitializeSourceDomaenenDB">
>                <provider>
>
> org.apache.openjpa.persistence.PersistenceProviderImpl
>                </provider>
>
> <jta-data-source>java:comp/env/jdbc/domaenendb</jta-data-source>
>                <properties>
>                        <property name="openjpa.TransactionMode"
> value="managed"/>
>                <property name="openjpa.ConnectionFactoryMode"
> value="managed"/>
>                <property name="openjpa.jdbc.DBDictionary" value="db2"/>
>                <property name="openjpa.jdbc.Schema" value="XX"/>
>                </properties>
>        </persistence-unit>
> </persistence>
>
>
>
> Executed code
>
>        DomainAssociation da = new DomainAssociation();
>        da.setName("da");
>        da.setComment("comment");
>
>        Domain dom1 = new Domain();
>        dom1.setName("domain1");
>        dom1.setUse("PRES");
>        dom1.setType("String");
>        dom1.setComment("comment");
>        dom1.setDomainAssociation(da);
>
>        Domain dom2 = new Domain();
>        dom2.setName("domain2");
>        dom2.setUse("MOD");
>        dom2.setType("String");
>        dom2.setComment("comment");
>        dom2.setDomainAssociation(da);
>
>        List<Domain> domains = new Vector<Domain>();
>        domains.add(dom1);
>        domains.add(dom2);
>        da.setDomains(domains);
>
>        this.getEm().persist(da);
>
>
>
>
> Thrown Exception:
>
>
> <openjpa-1.0.2-r420667:627158 nonfatal general error>
> org.apache.openjpa.persistence.PersistenceException: DB2 SQL error:
> SQLCODE: -530, SQLSTATE: 23503, SQLERRMC: T7.TADOMAIN.FK_01 {prepstmnt
> 1699374410 INSERT INTO T7.TADOMAIN (DOMAIN_NAME, DOMAIN_BEMERKUNG,
> DOMAIN_TYP, DOMAIN_VERWENDUNG, DA_NAME) VALUES (?, ?, ?, ?, ?)
> [params=(String) domain2, (String) comment, (String) String, (String)
> MOD, (String) da]} [code=-530, state=23503]SQLCA OUTPUT[Errp=SQLRI079,
> Errd=-2145779603, 0, 0, 0, -100, 0]
>
>
>
>
> Dear greetings
> Annette Scherer
>
>
> ____________________________________________________________
>
> * This email and any files transmitted with it are CONFIDENTIAL and
> intended
>  solely for the use of the individual or entity to which they are
> addressed.
> * Any unauthorized copying, disclosure, or distribution of the material
> within
>  this email is strictly forbidden.
> * Any views or opinions presented within this e-mail are solely those of
> the
>  author and do not necessarily represent those of Odyssey Financial
> Technologies SA unless otherwise specifically stated.
> * An electronic message is not binding on its sender. Any message
> referring to
>  a binding engagement must be confirmed in writing and duly signed.
> * If you have received this email in error, please notify the sender
> immediately
>  and delete the original.
>
>

Mime
View raw message