-->
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.  [ 4 posts ] 
Author Message
 Post subject: How do I override the targetEntity
PostPosted: Wed Oct 07, 2009 2:34 pm 
Newbie

Joined: Wed Jun 17, 2009 5:27 pm
Posts: 9
I've created a couple of base class master / detail entity beans. Now I want to descend from them. In order to maintain the new master / detail relationship I need to change the targetEntity in the descendant. How is this done?


Top
 Profile  
 
 Post subject: Re: How do I override the targetEntity
PostPosted: Wed Oct 07, 2009 8:22 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi prestonfm,

what do you want to do? Could you probably be more precise and possibly provide some code?

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: How do I override the targetEntity
PostPosted: Thu Oct 08, 2009 11:50 am 
Newbie

Joined: Wed Jun 17, 2009 5:27 pm
Posts: 9
I have two separate databases that contain a report and reportItems table. The table structure is the same for both databases.

I have a session bean that needs to write to each database / table. In order to keep the code simple I want the session bean to work with a base entity bean that then goes to the appropriate table.

To accomplish that, I've created a base entity class call Report and another called ReportItem. The Report class contains a OneToMany relation for the ReportItem class.

Code:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Report implements Serializable {
  ...   

  @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL},targetEntity=ReportItem.class)
  @JoinColumn(name="Report_ID", nullable=false)
  private Collection<ReportItem> reportItems = new ArrayList<ReportItem>();

... (set all the other properties)

}


From the Report and ReportItem classes I've descended two additional classes.

Report -> Database1Report & Database2Report

ReportItem -> Database1ReportItem & Database2ReportItem

The base class does not contain a @Table because I really don't want it to write to the database so I declare it in the descendant classes accordingly.

Code:
@Entity
@Table(schema= "Database1", name = "reports")
public class Database1Report extends Report implements Serializable {
    public Database1Report() {}
}


The problem is that now I need my Database1Report class to use Database1ReportItem for it's collection. Without being able to override the targetEntity in the descendant class I don't know how to change the collection that was defined in the base entity class.

I'd like to do something like the AssociationOverrride but it doesn't work for targetEntity

Code:
@AssociationOverride( name="report", targetEntity=Database1Report.class)


Top
 Profile  
 
 Post subject: Re: How do I override the targetEntity
PostPosted: Sat Oct 10, 2009 3:19 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi prestonfm,

ok, that's better now.

I'm afraid, you have to use Java generics to get to the solution. Something like
Code:
public class Report<T extends ReportItem> implements ... {
...
private Collection<T> reportItems = new ArrayList<T>();
...
}

Then, create your subclasses as, i.e.,
Code:
public class Database1Report<Database1ReportItem> implements ... {
...
}
Java type erasure gets your subclass collections to be limited to the corresponding ReportItem sub-class.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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