Hello,
I'm getting lost trying to map an association class with extra attributes. I found some mapping exemple on the net but i got a problem when i try to merge the entities in cascade.
I use this code :
Code:
@Test
public final void testAddingTravelWithMember(){
TravelRequest travelRequest = new TravelRequest();
RequestMember requestMember = new RequestMember();
requestMember.setTravelRequest(travelRequest);
requestMember.setMember(member);
travelRequest.getRequestMembers().add(requestMember);
member.getRequestMembers().add(requestMember);
travelRequest = emTest.update(travelRequest);
Assert.assertNotNull(travelRequest.getTravelRequestId());
}
and I got the following exception :
Code:
javax.persistence.EntityNotFoundException: Unable to find com.traveldoo.ejb.entities.RequestMember with id com.traveldoo.ejb.entities.RequestMemberPk@a0c486
at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:133)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:233)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
...
I use this code :
Code:
@Entity
@Table(name = "TRAVEL_REQUEST")
public class TravelRequest implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "TRAVEL_REQUEST_ID", unique = true, nullable = false, precision = 10)
@GeneratedValue(generator = "SEQUENCE_TRAVEL_REQUEST_ID")
@SequenceGenerator(name = "SEQUENCE_TRAVEL_REQUEST_ID", sequenceName = "SEQUENCE_TRAVEL_REQUEST")
private Long travelRequestId;
...
@OneToMany(mappedBy = "travelRequest")
private List<RequestAction> actions = new ArrayList<RequestAction>();
@OneToMany(mappedBy = "travelRequest")
private List<RequestMember> requestMembers = new ArrayList<RequestMember>();
...
The association entity :
Code:
@Entity
@Table(name = "REQUEST_MEMBERS")
public class RequestMember implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private RequestMemberPk pk = new RequestMemberPk();
@ManyToOne
@JoinColumn(name = "TRAVEL_REQUEST_ID", insertable = false, updatable = false)
private TravelRequest travelRequest;
@ManyToOne
@JoinColumn(name = "MEMBER_ID", insertable = false, updatable = false)
private Member member;
...
the primary key :
Code:
@Embeddable
public class RequestMemberPk implements Serializable {
private static final long serialVersionUID = 5089975622327078543L;
@Column(name = "TRAVEL_REQUEST_ID", nullable = false)
private Long travelRequestId;
@Column(name = "MEMBER_ID", nullable = false)
private Long memberId;
...
The other entity used in pk :
Code:
@Entity
@Table(name = "MEMBER")
public class Member implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "MEMBER_ID", unique = true, nullable = false, precision = 10)
@GeneratedValue(generator = "SEQUENCE_MEMBER_ID")
@SequenceGenerator(name = "SEQUENCE_MEMBER_ID", sequenceName = "SEQUENCE_MEMBER")
private Long memberId;
...
@OneToMany(mappedBy = "member", cascade = ALL)
private List<Profile> profiles = new ArrayList<Profile>();
@OneToMany(mappedBy = "member")
private List<RequestMember> requestMembers = new ArrayList<RequestMember>();
...
If you got some ideas, please tell me!! ;)