-->
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.  [ 2 posts ] 
Author Message
 Post subject: hibernate does not insert an array property
PostPosted: Wed Apr 12, 2006 12:06 pm 
Newbie

Joined: Fri Mar 24, 2006 9:31 am
Posts: 10
Location: Berlin, Germany
I tried to persist a class A with an array typed property. The array is of type B. When I construct an object of type A with instances of type B in its array property hibernate tries to update the collection instead of inserting it.

What do I wrong ?

Hibernate version:
3.2 alpha1

Class code:

Code:
package array;
public class A {
    int id;
    int version;
    B[] ba;
    public final B[] getBa() {
        return ba;
    }
    public final void setBa(B[] ba) {
        this.ba = ba;
    }
    public final int getId() {
        return id;
    }
    public final void setId(int id) {
        this.id = id;
    }
    public final int getVersion() {
        return version;
    }
    public final void setVersion(int version) {
        this.version = version;
    }
}

Code:
package array;
public class B {
    int id;
    int version;
    double value;
    public final int getId() {
        return id;
    }
    public final void setId(int id) {
        this.id = id;
    }
    public final double getValue() {
        return value;
    }
    public final void setValue(double value) {
        this.value = value;
    }
    public final int getVersion() {
        return version;
    }
    public final void setVersion(int version) {
        this.version = version;
    }
}



Mapping documents:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="array.B" lazy="false" table="b">
        <id name="id" type="int" column="id">
            <generator class="native" />
        </id>
        <version name="version" type="int" column="version"/>
        <property name="value"/>
    </class>
    <class name="array.A" lazy="false" table="a">
        <id name="id" type="int" column="id">
            <generator class="native" />
        </id>
        <version name="version" type="int" column="version"/>
        <array name="ba" table="b">
            <key column="id" />
            <index column="bindex" />
            <one-to-many class="array.B" not-found="ignore" />
        </array>
    </class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Code:
        session.beginTransaction();

        A a = new A();

        B[] ba = new B[5];

        for (int i = 0; i < 5; i++) {
            ba[i] = new B();
            ba[i].value = i * 3.4;
        }

        a.setBa(ba);

        session.save(a);
        session.getTransaction().commit();


Full stack trace of any exception that occurs:

Code:
12.04.2006 15:30:06 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SCHWERWIEGEND: Could not synchronize database state with session
org.hibernate.TransientObjectException: array.B
   at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
   at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
   at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:71)
   at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:697)
   at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1037)
   at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at array.Main.main(Main.java:43)
Exception in thread "main" org.hibernate.TransientObjectException: array.B
   at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
   at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
   at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:71)
   at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:697)
   at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1037)
   at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at array.Main.main(Main.java:43)



Name and version of the database you are using:
Code:
Oracle, version: Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options


The generated SQL (show_sql=true):
Code:
Hibernate: select obi.hibernate_sequence.nextval from dual
Hibernate: insert into obi.a (version, id) values (?, ?)
Hibernate: update obi.b set id=?, bindex=? where id=?


Top
 Profile  
 
 Post subject: The Clue is..
PostPosted: Tue May 02, 2006 8:47 am 
Newbie

Joined: Fri Mar 24, 2006 9:31 am
Posts: 10
Location: Berlin, Germany
I found out that the array mapping must have a cascade="true" then the transient array gets saved with its parent object.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.