Hello,
I can't map a component containing just an array of 2 properties.
I tryied with Hibernate 3.1
In my example, a BeanNetwork has a member line
which is a component contains an array of route (a route is a couple of string properties).
Does anybody know how to do it ?
Thanks a lot to help me.
I give all the details of my test below:
Ihe only mapping file (BeanNetwork.hbm.xml) is here,
<class name="test.hibernate.BeanNetwork" table="network" dynamic-update="false" dynamic-insert="false"> <id name="id" column="id" type="long"> <generator class="native"/> </id> <property name="name" type="string" update="true" insert="true" access="property" not-null="true" column="messageFlash" /> <component name="line" class="test.hibernate.Line"> <array name="Routes" table="line_route"> <key column="id"/> <list-index column="position"/> <composite-element class="test.hibernate.Route"> <property name="name" column="name" not-null="true"/> <property name="company" column="company" not-null="true"/> </composite-element> </array> </component> </class>
Here is the test I've launched (in a junit TestCase class):
//--------------------------------------------------
public void test_create() { Route routeA = new Route(); Route routeB = new Route(); Route[] routeArray = new Route[]{ routeA, routeB}; routeA.setCompany( "Comp A"); routeA.setName( "route A"); routeB.setCompany( "Comp B"); routeB.setName( "route B"); Line line = new Line(); BeanNetwork network = new BeanNetwork(); Session aSession = null; Transaction aTransaction = null; try { line.setRoutes( routeArray); network.setName( "Network 1"); network.setLine( line); // Create a Sesssion aSession = _session_factory.openSession();
// Open a transaction aTransaction = aSession.beginTransaction();
// Save aSession.save( network);
// Commit transaction, aTransaction.commit(); // An Exception is raised HERE !!
//--------------------------------------------------
The Full stack trace I've got is just bellow:
java.lang.NullPointerException at org.hibernate.engine.Collections.processReachableCollection(Collections.java:147) at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:37) at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101) at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61) at org.hibernate.event.def.AbstractVisitor.processValues(AbstractVisitor.java:40) at org.hibernate.event.def.AbstractVisitor.processComponent(AbstractVisitor.java:82) at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:107) at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61) at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55) at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:124) at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:353) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at test.hibernate.TestNetwork.test_create(TestNetwork.java:150) 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:324) 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 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
My 3 classes: Route, Line and BeanNetwork:
//--------------------------------------------------
package test.hibernate; public class Route { private String name = null; private String company = null;
public Route() { super(); } public String getCompany() { return company; } public void setCompany( String company) { this.company = company; } public String getName() { return name; } public void setName( String name) { this.name = name; } }
//--------------------------------------------------
package test.hibernate;
import java.util.Vector;
public class Line { private java.util.Vector routeInfo; public Line() { super(); routeInfo = new Vector(); } public void setRoutes(Route[] routeArray) { //-- copy array routeInfo.removeAllElements(); for (int i = 0; i < routeArray.length; i++) { routeInfo.addElement(routeArray[i]); } } public Route[] getRoutes() { int size = routeInfo.size(); Route[] mArray = new Route[size]; for (int index = 0; index < size; index++) { mArray[index] = (Route) routeInfo.elementAt(index); } return mArray; } }
//--------------------------------------------------
package test.hibernate;
public class BeanNetwork { private long id = 0L; private String name = null; private Line line = null;
public BeanNetwork() { super(); } public long getId() { return id; } public void setId( long id) { this.id = id; } public Line getLine() { return line; } public void setLine( Line line) { this.line = line; } public String getName() { return name; } public void setName( String name) { this.name = name; } }
//--------------------------------------------------
As it may help, I have copied the Hibernate log below:
2006-01-02 12:38:57,071 [main] DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: false 3154 [main] DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: false 2006-01-02 12:38:57,071 [main] DEBUG org.hibernate.jdbc.JDBCContext - after transaction begin 3154 [main] DEBUG org.hibernate.jdbc.JDBCContext - after transaction begin 2006-01-02 12:38:57,071 [main] DEBUG org.hibernate.event.def.DefaultSaveOrUpdateEventListener - saving transient instance 3154 [main] DEBUG org.hibernate.event.def.DefaultSaveOrUpdateEventListener - saving transient instance 2006-01-02 12:38:57,071 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 3154 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 2006-01-02 12:38:57,071 [main] DEBUG org.hibernate.SQL - select nextval ('hibernate_sequence') 3154 [main] DEBUG org.hibernate.SQL - select nextval ('hibernate_sequence') Hibernate: select nextval ('hibernate_sequence') 2006-01-02 12:38:57,071 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement 3154 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement 2006-01-02 12:38:57,081 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: 1 3164 [main] DEBUG org.hibernate.id.SequenceGenerator - Sequence identifier generated: 1 2006-01-02 12:38:57,081 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 3164 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 2006-01-02 12:38:57,081 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement 3164 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement 2006-01-02 12:38:57,081 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener - generated identifier: 1, using strategy: org.hibernate.id.SequenceGenerator 3164 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener - generated identifier: 1, using strategy: org.hibernate.id.SequenceGenerator 2006-01-02 12:38:57,081 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener - saving [test.hibernate.BeanNetwork#1] 3164 [main] DEBUG org.hibernate.event.def.AbstractSaveEventListener - saving [test.hibernate.BeanNetwork#1] 2006-01-02 12:38:57,131 [main] DEBUG org.hibernate.transaction.JDBCTransaction - commit 3214 [main] DEBUG org.hibernate.transaction.JDBCTransaction - commit 2006-01-02 12:38:57,131 [main] DEBUG org.hibernate.impl.SessionImpl - automatically flushing session 3214 [main] DEBUG org.hibernate.impl.SessionImpl - automatically flushing session 2006-01-02 12:38:57,131 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - flushing session 3214 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - flushing session 2006-01-02 12:38:57,141 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades 3224 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades 2006-01-02 12:38:57,152 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections 3235 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections 2006-01-02 12:38:57,152 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushing entities and processing referenced collections 3235 [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushing entities and processing referenced collections 2006-01-02 12:38:57,162 [main] DEBUG org.hibernate.transaction.JDBCTransaction - rollback 3245 [main] DEBUG org.hibernate.transaction.JDBCTransaction - rollback 2006-01-02 12:38:57,162 [main] DEBUG org.hibernate.transaction.JDBCTransaction - rolled back JDBC Connection 3245 [main] DEBUG org.hibernate.transaction.JDBCTransaction - rolled back JDBC Connection 2006-01-02 12:38:57,162 [main] DEBUG org.hibernate.jdbc.JDBCContext - after transaction completion 3245 [main] DEBUG org.hibernate.jdbc.JDBCContext - after transaction completion 2006-01-02 12:38:57,162 [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection 3245 [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection 2006-01-02 12:38:57,162 [main] DEBUG org.hibernate.jdbc.ConnectionManager - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 3245 [main] DEBUG org.hibernate.jdbc.ConnectionManager - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 2006-01-02 12:38:57,162 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1 3245 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1 2006-01-02 12:38:57,162 [main] DEBUG org.hibernate.impl.SessionImpl - after transaction completion 3245 [main] DEBUG org.hibernate.impl.SessionImpl - after transaction completion 2006-01-02 12:38:57,172 [main] DEBUG org.hibernate.impl.SessionImpl - closing session 3255 [main] DEBUG org.hibernate.impl.SessionImpl - closing session 2006-01-02 12:38:57,172 [main] DEBUG org.hibernate.jdbc.ConnectionManager - connection already null in cleanup : no action 3255 [main] DEBUG org.hibernate.jdbc.ConnectionManager - connection already null in cleanup : no action
|