-->
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.  [ 4 posts ] 
Author Message
 Post subject: many-to-many using standard examples just won't work!(mysql)
PostPosted: Thu Oct 14, 2004 10:21 pm 
Newbie

Joined: Thu Oct 14, 2004 6:15 am
Posts: 2
Hi.

I simply cannot get hibernate to work properly and I'm hoping someone can help. It works fine if I use a many-to-one relationship, but many-to-many won't work. I decided to cut & paste the standard examples from the tutorial and have the same problem, so at least I know it's not the code. There must be something else I just don't get!

Any help *really* appreciated!

Maria

-------------------------------------------------------
The standard code copied from tutorial:
public class Parent {
private long id;
private Set children;

public long getId() { return id; }
private void setId(long id) { this.id=id; }

private Set getChildren() { return children; }
private void setChildren(Set children) { this.children=children; }

}
public class Child {
private long id;
private String name;

public long getId() { return id; }
private void setId(long id) { this.id=id; }

public String getName() { return name; }
public void setName(String name) { this.name = name; }
}

-------------------------------------------------------
Hibernate mapping:
<hibernate-mapping>
<class name="net.kidzed.tests.Parent">
<id name="id">
<generator class="sequence"/>
</id>
<set role="children" lazy="true">
<key column="parent_id"/>
<one-to-many class="net.kidzed.tests.Child"/>
</set>
</class>

<class name="net.kidzed.tests.Child">
<id name="id">
<generator class="sequence"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>

-------------------------------------------------------
Table creation SQL:
create table parent ( id bigint not null primary key );
create table child ( id bigint not null primary key, name varchar(255) );
create table childset ( parent_id bigint not null references parent,
child_id bigint not null references child,
primary key ( parent_id, child_id ) );

-------------------------------------------------------
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- properties -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/kidzed</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>

<!-- mapping files -->
<mapping resource="net/kidzed/tests/Parent.hbm.xml"/>
<mapping resource="net/kidzed/model/data/Category.hbm.xml"/>
<mapping resource="net/kidzed/model/student/Student.hbm.xml"/>
<mapping resource="net/kidzed/model/user/User.hbm.xml"/>

<!--
<mapping resource="net/kidzed/model/student/PhysicalMeasurement.hbm.xml"/>
<mapping resource="net/kidzed/model/student/Observation.hbm.xml"/>
<mapping resource="net/kidzed/model/student/AcquiredKnowledgeSkill.hbm.xml"/>
-->

</session-factory>

</hibernate-configuration>

-------------------------------------------------------
The error:
INFO [main] (Environment.java:469) - Hibernate 2.1.6
INFO [main] (Environment.java:498) - hibernate.properties not found
INFO [main] (Environment.java:529) - using CGLIB reflection optimizer
INFO [main] (Configuration.java:895) - configuring from resource: /hibernate.cfg.xml
INFO [main] (Configuration.java:867) - Configuration resource: /hibernate.cfg.xml
DEBUG [main] (DTDEntityResolver.java:20) - trying to locate http://hibernate.sourceforge.net/hibern ... on-2.0.dtd in classpath under net/sf/hibernate/
DEBUG [main] (DTDEntityResolver.java:29) - found http://hibernate.sourceforge.net/hibern ... on-2.0.dtd in classpath
DEBUG [main] (Configuration.java:853) - connection.driver_class=com.mysql.jdbc.Driver
DEBUG [main] (Configuration.java:853) - connection.url=jdbc:mysql://localhost/kidzed
DEBUG [main] (Configuration.java:853) - dialect=net.sf.hibernate.dialect.MySQLDialect
DEBUG [main] (Configuration.java:853) - show_sql=true
DEBUG [main] (Configuration.java:1012) - null<-org.dom4j.tree.DefaultAttribute@bd928a [Attribute: name resource value "net/kidzed/tests/Parent.hbm.xml"]
INFO [main] (Configuration.java:331) - Mapping resource: net/kidzed/tests/Parent.hbm.xml
DEBUG [main] (DTDEntityResolver.java:20) - trying to locate http://hibernate.sourceforge.net/hibernate-mapping.dtd in classpath under net/sf/hibernate/
DEBUG [main] (DTDEntityResolver.java:25) - http://hibernate.sourceforge.net/hibern ... ing.dtdnot found in classpath
INFO [main] (Binder.java:229) - Mapping class: net.kidzed.tests.Parent -> Parent
DEBUG [main] (Binder.java:486) - Mapped property: id -> id, type: long
ERROR [main] (Configuration.java:295) - Could not configure datastore from input stream
java.lang.NullPointerException
at net.sf.hibernate.util.StringHelper.qualify(StringHelper.java:241)
at net.sf.hibernate.cfg.Binder.bindCollection(Binder.java:509)
at net.sf.hibernate.cfg.Binder$2.create(Binder.java:1434)
at net.sf.hibernate.cfg.Binder.propertiesFromXML(Binder.java:1028)
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:362)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1256)
at net.sf.hibernate.cfg.Configuration.add(Configuration.java:252)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:288)
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:336)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:1013)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:969)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:883)
at net.kidzed.db.HibernateUtil.<clinit>(HibernateUtil.java:28)
at net.kidzed.control.user.UserManager.findUser(UserManager.java:90)
at net.kidzed.control.user.UserManager.addUser(UserManager.java:167)
at net.kidzed.control.student.StudentManager_Test.setUp(StudentManager_Test.java:63)
at junit.framework.TestCase.runBare(TestCase.java:125)
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 junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
ERROR [main] (HibernateUtil.java:30) - Initial SessionFactory creation failed.
net.sf.hibernate.MappingException: Error reading resource: net/kidzed/tests/Parent.hbm.xml
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:339)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:1013)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:969)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:883)
at net.kidzed.db.HibernateUtil.<clinit>(HibernateUtil.java:28)
at net.kidzed.control.user.UserManager.findUser(UserManager.java:90)
at net.kidzed.control.user.UserManager.addUser(UserManager.java:167)
at net.kidzed.control.student.StudentManager_Test.setUp(StudentManager_Test.java:63)
at junit.framework.TestCase.runBare(TestCase.java:125)
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 junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Caused by: net.sf.hibernate.MappingException: java.lang.NullPointerException
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:296)
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:336)
... 20 more
Caused by: java.lang.NullPointerException
at net.sf.hibernate.util.StringHelper.qualify(StringHelper.java:241)
at net.sf.hibernate.cfg.Binder.bindCollection(Binder.java:509)
at net.sf.hibernate.cfg.Binder$2.create(Binder.java:1434)
at net.sf.hibernate.cfg.Binder.propertiesFromXML(Binder.java:1028)
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:362)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1256)
at net.sf.hibernate.cfg.Configuration.add(Configuration.java:252)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:288)
... 21 more


Top
 Profile  
 
 Post subject: NullPointerException
PostPosted: Fri Oct 15, 2004 3:32 pm 
Newbie

Joined: Tue Sep 21, 2004 6:10 pm
Posts: 16
Location: Toronto, Canada
I am running into the same problem.
I spend sometime dubugging it and found it to be related to the name attribute for the set element.
In my hbm file I am not specifying a name attribute and this is why hibernate throws the NullPointerException. However, when I specify it then I get an exception as follows:

Code:
15:18:28,964 ERROR XMLHelper:48 - Error parsing XML: XML InputStream(15) Attribute "name" must be declared for element type "set".
net.sf.hibernate.MappingException: Error reading resource: model/Landlord.hbm.xml
   at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:336)
   at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:990)
   at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:946)
   at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:874)
   at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:860)
   at model.DbSchemaViewer.main(DbSchemaViewer.java:36)
Caused by: net.sf.hibernate.MappingException: invalid mapping
   at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:284)
   at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:333)
   ... 5 more
Caused by: org.xml.sax.SAXParseException: Attribute "name" must be declared for element type "set".
   at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
   at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
   at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
   at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
   at org.apache.xerces.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(Unknown Source)
   at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
   at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
   at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
   at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
   at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
   at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
   at org.dom4j.io.SAXReader.read(SAXReader.java:339)
   at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:283)
   ... 6 more
Exception in thread "main"


which is kind of weird as I am specifying the name attribuute.

If I don't specify the name attribute then I get exactly the same exception as Maria.

the hbm.xml file looks like this:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 1.1//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-1.1.dtd">

<hibernate-mapping>

<class name="model.Landlord" table="Landlord">
<id name="id" column="id" type="int">
<generator class="native"></generator>
</id>

<set role="properties" table="LandlordProperty" lazy="true"
readonly="false" cascade="none" sort="unsorted">

<key column="landlordId" />

<many-to-many class="Property" column="propertyId"
outer-join="auto" />

</set>

</class>

</hibernate-mapping>

I'll be monitoring this topic waiting for some help as well.

Thanks in advance..

Erick


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 3:36 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The fact that you are using a Hibernate 1.1. mapping with Hibernate 2.1.6 should give you enough pointers :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 5:43 pm 
Newbie

Joined: Thu Oct 14, 2004 6:15 am
Posts: 2
Thankyou!!!!


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