-->
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.  [ 3 posts ] 
Author Message
 Post subject: Having Multiple RelationShips in a Entity
PostPosted: Sat Jun 23, 2012 10:14 am 
Newbie

Joined: Sat Jun 23, 2012 10:03 am
Posts: 3
I am new to Hibernate.

I wanted to know if I can do this, meaning have 2 onetoone relationships in an entity. Each one to the different entity. This seems not to be working for me when I try to access the values from linStatus, I am getting a null value for a field in Status entity.

Basically, I am trying to do this

Order Entity ---> LItems Entity ---> Status ENtity. I am trying to get the status for an order, an order can contain multiple Items in it. But I am getting a null value for item's statuses


@Entity
@Table(name = "Litem")
public class Litem {

@Id
@Column(name = "item")
private Long id;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "item")
private Source lineSources;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "item")
private Status lineStatus;

...getters and setters...
}


Top
 Profile  
 
 Post subject: Re: Having Multiple RelationShips in a Entity
PostPosted: Sat Jun 23, 2012 9:06 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
I see no reason why you can't have several @OneToOne relations in a single entity, but in this case I think your object model is wrong (Or may be I dind't understad what you are trying to do)

If an order can contain several items, then this is not a @OneToOne relation, but a @OneToMany relation in the Order side, and a @ManyToOne in the LItem side.

I also suppose you have a limited set of status, so that several LItems may have the same status. If that is the case, then use @ManyToOne in LItem to relate with Status, and @OneToMany in Status to relate with LItem


Top
 Profile  
 
 Post subject: Re: Having Multiple RelationShips in a Entity
PostPosted: Sun Jun 24, 2012 8:18 am 
Newbie

Joined: Sat Jun 23, 2012 10:03 am
Posts: 3
I have total 3 tables or entities.

1. @Order - For one Order # there can be Multiple LineItems
- For One Order there can be multiple emails too. (This part is working so dont need to look at this)

2. @LineItem - For One Line Item there can be Only 1 LinStatus
3. @LineItem - For One LineItem there can be Only 1 LinFileInfos

4. @LinStatus

5. @LinFileInfos


@Order has the following relationships

@Entity
@Table(name = "order_sb")
public class Order extends AbstractModel<Long> {

@Id
@Column(name = "order_int")
private Long id;

@Column(name = "order_tod")
private Date orderTodate;

@Column(name = "order_type")
private String orderType;

@Column(name = "activity_site")
private String activitySite;

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", insertable = false, updatable = false)
private UserReg userId;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "order_int", insertable = false, updatable = false)
private List<OrderEmail> orderEmail;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "order_int", insertable = false, updatable = false)
List<LineItem> lineItems;
...getters and setters...

}


@Entity
@Table(name = "line_item_sb")
public class LineItem extends AbstractModel<Long> {

@Id
@Column(name = "item_int")
private Long id;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "item_int", insertable = false, updatable = false)
private LinSource lineSources;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "item_int", insertable = false, updatable = false, referencedColumnName = "item_int")
private LinStatus lineStatus;
...getters and setters ...

@Entity
@Table(name = "lin_status_sb")
public class LinStatus extends AbstractModel<Long> {

@Id
@GeneratedValue
@Column(name = "lin_status_id")
private Long id;

@Column(name = "item_int")
private Long itemId;

@Column(name = "state_tod")
private Date stateTodate;

@Column(name = "item_status")
private Long itemStatus;
...getters and setters ...
}


@Entity
@Table(name = "lin_file_info_sb")
public class LinSource extends AbstractModel<Long> {

@Id
@GeneratedValue
@Column(name = "lin_file_info_id")
private Long id;

@Column(name = "item_int")
private Long itemId;
...getters and getters ....
}

LinStatus and LiFileInfo dont have any relationships

problem is when I am trying to get to LInStatus from LineItem. The Hibernate is generating the join on a wrong field for example

linstatus1_.item_status as item3_2_0_,
linstatus1_.state_tod as state4_2_0_,
linsource2_.lin_file_info_id as lin1_1_1_,
linsource2_.dataset_name as dataset2_1_1_,
linsource2_.datatype_name as datatype3_1_1_,
linsource2_.item_int as item4_1_1_
from
line_item_sb lineitems0_
left outer join
lin_status_sb linstatus1_
on lineitems0_.item_int=linstatus1_.lin_status_id
left outer join
lin_file_info_sb linsource2_
on lineitems0_.item_int=linsource2_.lin_file_info_id
where
lineitems0_.order_int=?



the lines: on lineitems0_.item_int=linstatus1_.lin_status_id

should be
on lineitems0_.item_int=linstatus1_.item_int


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