-->
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.  [ 6 posts ] 
Author Message
 Post subject: Null Collections
PostPosted: Thu Feb 15, 2007 1:29 pm 
Beginner
Beginner

Joined: Thu Oct 06, 2005 7:34 pm
Posts: 20
Is there a way to get a collection mapped in hibernate to load a null value instead of an empty collection if there is no data for the collection in the database?

From the example below there is no data for the keywords list. I would like the PortletInfoImpl created by hibernate to set null for the keywords property instead of an empty list as they have two different meanings for this application.

Hibernate version: 3.2.0-ga

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 default-lazy="false">
    <class name="org.jasig.portal.portlet.transfer.impl.PortletDefinitionTransferImpl"
        table="UP_PORT_DEF">
       
        <id name="portletDefinitionId" type="org.jasig.portal.portlet.dao.hibernate.types.PortletDefinitionIdUserType">
            <column name="PORT_DEF_ID"/>
            <generator class="assigned"/>
        </id>
        <component name="portletInfo" class="org.jasig.portal.portlet.common.PortletInfoImpl">
            <property name="title" type="string">
                <column name="INFO_TITLE"/>
            </property>
            <property name="shortTitle" type="string">
                <column name="INFO_SHORT_TITLE"/>
            </property>
            <list name="keywords" table="UP_PORT_DEF_INFO_KEYWORDS" cascade="all">
                <key>
                    <column name="PORT_DEF_ID"/>
                </key>
                <index column="SORT_ORDER"/>
                <element type="string">
                    <column name="KEYWORD" not-null="true"/>
                </element>             
            </list>
        </component>
    </class>
</hibernate-mapping>


Name and version of the database you are using: HSQL 1.8.0.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 3:37 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
can you just extend the POJO, overwrite the getter, and return null if it's empty

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 4:01 pm 
Beginner
Beginner

Joined: Thu Oct 06, 2005 7:34 pm
Posts: 20
If I do that hibernate complains when populating a List with content since its operation is to set an empty PersistentList then get it each time it adds an item. The first get will always return null which is a problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 4:18 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
True. Well I'm not sure there is a way to have hibernate do what you want to.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 1:11 am 
Newbie

Joined: Wed Aug 24, 2005 1:43 pm
Posts: 10
what you could do is have two get methods:

protected List children = new ArrayList();

public List getChildren()
{
if (children.size() == 0) return null;
return children;
}

protected List getChildrenHib()
{
return children;
}

protected void setChildrenHib(List children)
{
this.children = children;
}

The protected methods are just for hibernate purposes, so you'd use childrenHib in your mapping files, and your client code can use the public one.

Cheers!
Seb


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 5:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
first of all null and isEmpty() is NOT the same - why would you ever want to make that the same ?

Anyhow, no need for tricks like getXXXHib() methods - just use access="field"

_________________
Max
Don't forget to rate


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