-->
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.  [ 14 posts ] 
Author Message
 Post subject: How can Hibernate persist a non-primitive class ????
PostPosted: Tue Aug 17, 2004 9:32 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
Hi, I have worked with JDO and I am new to Hibernate.
I need your help understanding the way of making class persistent with Hibernate.

Let say I have a class :
Code:
Employee
    private int           empNumber;
    private String        empName;
    private EmpAddr    empAddress; // <--- 


As I understand, I have 3 (??) ways of persisting this class :
1) save empAddress in a different table (in db) with relation column to Employee
2) save empAddress as is as an object inside Employee (let say, to a blob in db)
3) save calculated column such as empAddress.toString() to a varchar in db (inside Employee table)


Am I correct with my assumptions ???

Can someone provide me with the syntax for mapping these 3 ways ? ( P L E A S E ......)?





Hibernate version:2.1.6
Name and version of the database you are using:Oracle 8i,9i[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 9:34 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
3) is the correct way to do this, we call this a <component> mapping.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 9:49 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
Thanx Christian,
In that case you can say that 3 is the correct way, but in other cases I might need to save the whole object AS-IS (for complex classes or classes which are not mine)

that is why I do wish to know the syntax of All 3 ways of doing it. that would help me much !!!!

as for using <component>, from what I saw on the hibernate_reference, I use it in order to describe how the internal class (empAddress) looks like and not to use any method like toString of empAddress.....

or I am wrong here also ..?????


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 9:52 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Uhm, I see no way explaining this better, it's already pretty trivial. I don't know why "not own classes" should be different or what "complex" means.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 10:03 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
sorry if my question is too basic.....
anyway,
when I mean complex, I mean that I have a Class
Code:
Class xxx
    private HashMap   memberA;
    private Document memberB; //org.w3c.dom.document
    private int            memberC;


now....
I want to save this class in Oracle in table XXX
and :
a) I dont know how Document should be so i will use blob in db
b) I want to save the HashMap as is so when i read the xxx class from
db I will be able the read the memberA directly to new HashMap....

Is it more clear now ????




[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 10:07 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Code:
<property name="memberC" type="int"/>
<property name="memberB" type="serializable"/>
<map name="memberA" table="MEMBER_A">
   <key column="XXX_ID"/>
   <index column="MAP_IDX" type="type_of_your_map_key"/>
   <element column="MAP_VALUE" type="type_of_your_map_value"/>
</map>


You know, this is basic stuff covered in the documentation.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 11:35 am 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
Sorry, I know it is basic stuff and I hoped someone could share some example for these situations. I have the hibernate_reference.pdf and it is lake of examples......

as for using a map, I already tried that and im struggling with this basic stuff !

when I used access="field" I got :
could not set a field value by reflection setter of test.hibernate.Nir.HM
when I used access="property" (default)
and just put setters as :
Code:
public HashMap getHM() {
     return HM;
}
public void setHM(HashMap hm) {
    HM=hm;
    //HM.clear();
    //HM.putAll(hm);   another option i tried
}


I got:
18:22:15,271 ERROR BasicPropertyAccessor:60 - IllegalArgumentException in class: test.hibernate.Nir, setter method of property: HM
18:22:15,271 ERROR BasicPropertyAccessor:64 - expected type: java.util.HashMap, actual value: net.sf.hibernate.collection.Map
net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of test.hibernate.Nir.HM
at net.sf.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:68)
at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:222)


[b]when I used Map instead of HashMap I did not get that problem.[b]

anyone ????


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 11:46 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
mooki wrote:
Sorry, I know it is basic stuff and I hoped someone could share some example for these situations. I have the hibernate_reference.pdf and it is lake of examples......


hibernate_reference.pdf is not for examples, have a look at http://www.hibernate.org/152.html instead.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 11:52 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
mooki wrote:
when I used Map instead of HashMap I did not get that problem.

anyone ????

oh yes to answer that question here's hibernates source for Map, which is what was expected.

Code:
public class Map extends PersistentCollection implements java.util.Map, DMap {


that's why java.util.Map was fine and not HashMap.

have ant generate your POJO's and you won't have that problem.
There's a very good tutorial on the link I posted that will take you through everything.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 12:02 pm 
Newbie

Joined: Thu Aug 12, 2004 9:32 am
Posts: 17
Thanx tj,
I dont know YET what is POJO's but I hope the i will find it in the links you sent.

Thanx.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 12:30 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
pojo (plain old java object)= (business) javabean that is to say you have persistent properties + business rules but without dependencies to another external package

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 4:51 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
christian wrote:
3) is the correct way to do this, we call this a <component> mapping.


His suggestion of using toString() is NOT the same as a <component> mapping. Using toString during a save would require parsing during the load, which could easily break (think: what separator would be used? You'd have to escape that separator in the generated string).

Using a "real" <component> mapping would work, obviously. I myself would prefer a one-to-many mapping to be more flexible in the future whe you have to support home, work, vacation addresses.

_________________
--DP


Top
 Profile  
 
 Post subject: Help mapping a HashMap property.
PostPosted: Thu Oct 07, 2004 3:41 pm 
Newbie

Joined: Fri Sep 10, 2004 10:21 am
Posts: 7
I'm having a difficult time mapping a HashMap property. My class "Account" has a hash map of "Payments", which is another class.

My class looks like this...
Code:
public class Account () {
   ...
   private Map payments = new HashMap();

   ...
   public Map getPayments() {
      return this.payments;
   }

   public void setPayments(Map payments) {
      this.payments = payments;
   }
}

May mapping files look like this...
Code:
<class name="Account" table="accounts">
   ...
   <map name="payments" table="payments" cascade="all" access="property" >
        <key column="account_id"/>
       <index column="reference_number" type="string"/>
        <one-to-many class="Payment"/>
    </map>
</class>

<class name="Payment"  table="payments">
   ...
    <property name="referenceNumber" column="reference_number" type="string" length="32" not-null="true" access="field"/>
    <property name="paymentAmount" column="payment_amount" type="big_decimal" access="field" />
</class>

In my Account class, when I add a payment, I use the payment reference number as the key for the HashMap, and the payment object itself as the value for the HashMap.
Code:
// Add the payment to the account's list of unapplied credit payments
this.payments.put(payment.getReferenceNumber(), payment);

And in the code that I call to persist the account I do the following...
Code:
  // get the hibernate session
  Session session = HibernateUtil.getSession();

  // Save the parts of the account that are separate objects in the DB
  session.save(account.getPayments());

  // Save the account
  session.save(account);
  session.flush();

The call to session.save() with the payments throws the exception: No persister for: java.util.HashMap.

So, I have two questions, 1) given what I've included here, am I doing something seriously wrong to cause the exception? 2) Does Hibernate support persisting maps with a class as the map 'value'?

Any insight or direction is greatly appreciated.


Top
 Profile  
 
 Post subject: And the answer is...
PostPosted: Thu Oct 07, 2004 5:37 pm 
Newbie

Joined: Fri Sep 10, 2004 10:21 am
Posts: 7
I think I have solved my own problem. After stairing at the screen for a while longer, after my first post, it finally hit me that I must loop through my HashMap and save the objects it contains! Now I understand the error message "no persistor for: java.util.HashMap". I don't want to save the container, only the objects contained within it.

<< in my best Homer Simpson voice >> Dhooo!

As my colleague commented, as I shared the answer with him,
Quote:
"in the words of Red Foreman from 'That '70's Show'.... 'You dumb a$$'"


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