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.  [ 8 posts ] 
Author Message
 Post subject: Mapping a simple array of Strings? (solved)
PostPosted: Wed Aug 23, 2006 12:07 am 
Newbie

Joined: Thu Jul 27, 2006 11:36 am
Posts: 10
Hibernate version: 3.1.3

I am trying to store an array of Strings from my POJO using hibernate. The POJO looks something like this:
Code:
public class Task {
   private Long id;
   private String owner;
   private Date creationDate;
   private String task;
   private String[] items;
...
}


The rest of the class is pretty easy to map but I can't figure out the array of strings. I tried using primitive-array but that did not work for some reason. Something about not handling an error. I also tried the array tag but that seemed to require another class which I do not have.

Here is the mapping without the items field:
Code:
<hibernate-mapping>
   <class name="edu.vt.lt.Task"
      table="VT_TASKS">
      <id name="id" type="long" column="TASK_ID">
          <generator class="native">
                <param name="sequence">TASK_ID_SEQ</param>
          </generator>
      </id>
      <property name="owner" type="string" length="255" not-null="true">
            <column name="TASK_OWNER"/>
        </property>
      <property name="creationDate" type="timestamp" not-null="true">
            <column name="TASK_CREATION_DATE"/>
        </property>   
      <property name="task" type="text" not-null="true">
            <column name="TASK_TEXT"/>
        </property>
   </class>   
</hibernate-mapping>


Is it possible to represent something like this in hibernate or do I just need to break down and create another object which is an id and a String? (that just seems wrong to me though)

Help?
-AZ


Last edited by azeckoski on Wed Aug 23, 2006 11:43 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 1:01 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You need another table with ids and strings, of course, but you don't need a java object. The mapping you need is this:
Code:
<array name="items" ...>
  <key column="id"/>
  <element type="string" column="col_string"/>
</array>

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 11:40 am 
Newbie

Joined: Thu Jul 27, 2006 11:36 am
Posts: 10
That was pretty close but it looks like this is what actually works:
Code:
<array name="items" table="TASK_ITEMS"
   inverse="true" cascade="all-delete-orphan">
   <key column="id" not-null="true"/>
   <list-index column="index"/>
   <element type="string" column="item" not-null="true"/>
</array>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 5:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
But my post didn't help solve the problem? Weird.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Persist the array without the other table
PostPosted: Mon Oct 09, 2006 4:56 pm 
Newbie

Joined: Wed Apr 05, 2006 4:31 pm
Posts: 4
Location: Brazil
tenwit wrote:
But my post didn't help solve the problem? Weird.

Onlye with a user type i can map the array to a column?

I would like to persist the array in a primary table's column. Creating a user type can i do this?

Hiberante doesn't offer native suporrt for this?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 5:08 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hibernate supports primitive arrays. I don't know if that's what you're looking for, as your questions are cryptic. If section 6.2 of the refdocs doesn't help you, create a new topic with more information, and perhaps a few examples, so that we can figure out what you're asking.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 8:36 pm 
Newbie

Joined: Wed Apr 05, 2006 4:31 pm
Posts: 4
Location: Brazil
tenwit wrote:
Hibernate supports primitive arrays. I don't know if that's what you're looking for, as your questions are cryptic. If section 6.2 of the refdocs doesn't help you, create a new topic with more information, and perhaps a few examples, so that we can figure out what you're asking.


Sorry, my problem only will be solve creating a new type! I want to map an String[] to a colmn of the database.
column: ["aaa","bbbb","cccc"]

Other times I want to ma Object[ ] to a column.
column: [0,5,2]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 09, 2006 8:43 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Still not enough information for me to parse the request. Are you saying that you want a table with one column, in which every row is just a single string?

May I repeat my earlier suggestions: read the refdocs section 6.2, and (this one is the important one) start a new topic. Don't hijack someone else's unless the subject is exactly the same.

_________________
Code tags are your friend. Know them and use them.


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