-->
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: What is the best way to map this?
PostPosted: Fri Jul 21, 2006 7:06 pm 
Beginner
Beginner

Joined: Tue Jan 06, 2004 4:51 pm
Posts: 48
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.2.0rc2

Name and version of the database you are using:PostgreSQL 8.0

I have three tables:

Code:
report (id serial not null primary key)

parameter (id serial not null primary key)

report_parameter (report_id integer fk, parameter_id integer fk, required boolean not null default false)



I want to map this into two classes, Report (Integer id, List parameters) and Parameter(Integer id, Boolean required). Is this possible? Or do I have to create a third class to ReportParameter(Report report, Parameter parameter, Boolean required)?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 23, 2006 8:45 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's not directly possible, but it's easy to fake it. Set it up as a <set> containing a <composite-element>, which in turn contains a <property> and a <many-to-one unique="true">. Specify the composite-element class as Property, and the many-to-one class as PropertyValue or somesuch. Then in the Property class, which contains the a PropertyValue member, use delegation to provide API access to the PropertyValue properties. Hibernate will use getPropertyValue(), but the API user will use isRequired(), which returns getPropertyValue().isRequired().

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 10:10 am 
Beginner
Beginner

Joined: Tue Jan 06, 2004 4:51 pm
Posts: 48
Thanks for the reply. I think that's what I tried doing (I am using Annotations), but I did something wrong. Can you please post a code example?

What I tried doing was:

Code:
@Entity
public class Report implements Serializable {
@Id
public Integer getId() {...}

@OneToMany
@Embedded
@AttributeOverrides){
      @AttributeOverride(name="required", column=@Column(name="required"))
})
public List<Parameter> getParameter() {...}
}

@Entity @Embeddable
public class Parameter implements Serializable {
@Id public Integer getId(){...}
public Boolean getRequired() {...}
}


Sorry... I am quite new to annotations so I'm probably doing something silly.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 24, 2006 5:49 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm afraid I know next to nothing about EJB3 annotations. I know that you'll need to specify the ParameterValue class (or whatever you're going to call the class that is in the many-to-one relationship with the embeddable Parameter class). You'll also have to mark as @Transient the Required property in the Parameter class, because that's not mapped (it gets its value from the Required property in the embedded ParameterValue class).

BTW I believe that the javabean convention for property accessors that return a boolean/Boolean value is "is<propname>", not "get<propname>".

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 10:15 am 
Beginner
Beginner

Joined: Tue Jan 06, 2004 4:51 pm
Posts: 48
I thought it was get for Boolean (since it can be null) and is for boolean. I just use the Netbeans refactoring to generate my accessors and it just uses get, anyway.

Thanks for the help.. I'll play around with it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 5:34 pm 
Beginner
Beginner

Joined: Tue Jan 06, 2004 4:51 pm
Posts: 48
I've played around with it, even trying @CollectionOfElements with @Embeddable and @SecondaryTable, but I still couldn't get it to work.

Does anyone have an annotation example that could help?


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.