Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to persist a class which extends List<>?
How to persist a class which extends List<>? [message #1027145] Tue, 26 March 2013 16:00 Go to next message
Jan Vleeshouwers is currently offline Jan VleeshouwersFriend
Messages: 2
Registered: March 2013
Junior Member
What annotations are necessary to persist a class which extends List<>?
For example:
@Entity
public class Walk extends ArrayList<Step> implements Serializable { ... }

with a straight entity class Step:
@Entity
public class Step implements Serializable { ... }

Both have simple auto-generated id fields and the appropriate equals() and hashCode() implementations.

I would like the following code to persist the walk including the inserted steps. The entity manager em is created from the factory.
Walk walk = new Walk();
walk.add(new Step());
walk.add(new Step());
walk.add(new Step());
em.getTransaction().begin();
em.persist(walk);
em.getTransaction().commit();

This persists the Walk object in a table which only has an ID-column. No Steps are persisted, nor any relationships. I would expect to need a @OneToMany annotation somewhere, to cascade the persist operation to the Step objects. But where?
Or am I trying to do something that is not possible?
Re: How to persist a class which extends List<>? [message #1028744 is a reply to message #1027145] Thu, 28 March 2013 17:21 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
It doesn't make sense from a JPA/entity point of view to extend a collection, and JPA cannot see any attributes internal to the ArrayList class to map to the database. Your Work entity needs an ID and a collection attribute holding Step entities. If you want to use work as a list or collection type, have it implement List and just wraps the step collection attribute.
Re: How to persist a class which extends List<>? [message #1029233 is a reply to message #1028744] Fri, 29 March 2013 10:58 Go to previous message
Jan Vleeshouwers is currently offline Jan VleeshouwersFriend
Messages: 2
Registered: March 2013
Junior Member
So I was trying to do something impossible.
Your suggestion works, I already assumed that would be the alternative.
Thanks for the reply!
Previous Topic:Refresh entity doesnt work with relationships
Next Topic:Refreshing an entity with a reference to a cache-isolated entity has side-effects
Goto Forum:
  


Current Time: Sat Nov 22 03:10:40 GMT 2014

Powered by FUDForum. Page generated in 0.08949 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software