-->
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.  [ 12 posts ] 
Author Message
 Post subject: NullpointerException in one-to-many
PostPosted: Sat Nov 15, 2003 6:03 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
Someone asked it before but didn't receive an answer:

I try to configure a one-to-many relation but when the hibernate session start-up and arrive to map my parent class the log give me a NullPointerException... why?

It says: org.xml.sax.SAXParseException: Element "set" does not allow "one-to-many" here.

This causes problems:
Code:
/**
* @hibernate.set
* @hibernate.collection-one-to-many
*  class="base.domainobjects.Address"
*
* @return
*/
public Set getAdresses() {
   return adresses;
}


As you see I started using XDoclet. When I start the task topdown.hbm2ddl, I receive a NullpointerException when the build script is trying to export the schema. I modified the Workshop example which I downloaded from this site.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 6:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Looks like you are missing an @hibernate.collection-key mapping


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 6:18 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
That was fast!

I think I found the reason.

hibernate.cfg.xml
Code:
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/liberty</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="show_sql">true</property>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

        <mapping resource="base/domainobjects/Address.hbm.xml"/>
        <mapping resource="base/domainobjects/Person.hbm.xml"/>
    </session-factory>


In the section mapping resource there were some other classes from which I have removed the tag @hibernate.class. It seems I must delete them in this xml file too.

If I still have problems with it, I come back here.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 7:04 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
Now it seems to work. But something is very strange. I have to use set instead of list in the XDoclet tag! If I use list as XDoclet tag, then it says: org.xml.sax.SAXParseException: Element "list" does not allow "one-to-many" here.

And the generated SQL script contains no foreign keys. Why not?

Is there a task to automatically update the schema in the database? What I get is a SQL script which I have to execute manually.

Here is the latest code snippet:
Code:
/**
    * @hibernate.set
    *  inverse="true"
    *  lazy="true"
    *  cascade="all"
    * @hibernate.collection-key
    *  column="personId"
    * @hibernate.collection-one-to-many
    *  class="base.domainobjects.Address"
    * @return
    */
public List getAdresses() {
   return adresses;
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 7:09 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
The missing foreign key was because of inverse = true.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 7:11 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
List is an indexed collection, you need to have
Code:
@hibernate.collection-index ...

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 7:19 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
epbernard wrote:
List is an indexed collection, you need to have
Code:
@hibernate.collection-index ...


If I do this, no foreign key is generated. If I use XDoclet tag "set", a foreign key is generated.

The Java type must be list. How do I map it correctly?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 7:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Probably something like that

Code:
/**
    * @hibernate.list
    *  lazy="true"
    *  cascade="all"
    * @hibernate.collection-key
    *  column="personIndex"
    * @hibernate.collection-index
    *  column="personId"
    * @hibernate.collection-one-to-many
    *  class="base.domainobjects.Address"
    * @return
    */
public List getAdresses() {
   return adresses;
}


If you inverse="true", you must have a many-to-one from the Address object to Person

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 8:54 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
Okay, that worked.

Now I need to map a byte array.

This didn't work:
Code:
/**
* @hibernate.primitive-array
* @return
*/
public byte[] getEncryptedString() {
   return encryptedString;
}


It says: org.xml.sax.SAXParseException: Element "primitive-array" requires additional elements.

Whats wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 4:14 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Well, I think there is a need for a goods fully documented and googleizable tutorial ;-)

Well, I don't use xdoclet myself but I would say that a primitive array is basicaly a indexed collection so you need @hibernate.collection-key and @hibernate.collection-index tags plus a @hibernate.collection-element one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 07, 2004 10:04 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
I was wrong. It works with @hibernate.primitive-array.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 07, 2004 10:19 am 
Regular
Regular

Joined: Sat Oct 11, 2003 11:13 am
Posts: 69
quereinsteiger wrote:
I was wrong. It works with @hibernate.primitive-array.


It SEEMED to work. What solved my problem is:
Code:
/**
* TODO
* @hibernate.property
*  type="binary"
*/
public byte[] getEncryptedPassword() {
   return encryptedPassword;
}
[/code][/quote]


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