Reputation: 303
I'm really new to Hibernate and this is my first app which is enterprise level. I'm stuck at one to many mapping. I did the mapping whole day but it doesn't give to correct table structure for me.
Here is the ER diagram I want to map
These are the Classes
Feed Class
@Entity
public class Feed {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
private long quantity;
//With getters and setters
}
Feed Order Details Class
@Entity
public class FeedOrderDetail {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private double unitPrice;
private long quantity;
@OneToMany(cascade=CascadeType.MERGE)
@JoinColumn(name="feed_id")
private List<Feed> feed = new ArrayList<Feed>();
//Getters and Setters
}
after deploying app I get the following table structure
My problems are why feed_id is in Feed table? Should I add same feed every time I add a feed order detail? (it isn't a good idea)
I can achieve this by moving @OneToMany annotation and attributes to Feed table. but if I move it to Feed class how can I represent feed order details in JSP pages?
I'm using spring with this project also.
Upvotes: 2
Views: 82
Reputation: 21576
I would suggest to have a object reference on both sides of the relationship.
So have a reference to List<FeedOrderDetail>
in Feed and have a single refernce to a Feed in your FeedOrderDetail
.
Class Feed:
@Entity
public class Feed {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
private long quantity;
//With getters and setters
// !new!
@OneToMany(mappedBy = "feed") // mappedBy references the fieldname in the other Java class
private List<FeedOrderDetail> details;
}
Class Detail:
@Entity
public class FeedOrderDetail {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private double unitPrice;
private long quantity;
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="feed_id")
private Feed feed; // only a single feed reference
//Getters and Setters
}
If you want to get a list of feeds using Hibernate's JPA API, then you can use this code:
TypedQuery<Feed> query = entityManager.createQuery("SELECT f FROM Feed f", Feed.class);
List<Feed> feeds = query.getResultList();
Upvotes: 1
Reputation: 4973
My problems are why feed_id is in Feed table?
You are right, it should not be in case your ER diagram is right.
Your ER diagram and table structure does not match, so I am trying to give you class mapping according to your ER-diagram.
Feed.java
@Entity
public class Feed {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
private long quantity;
//With getters and setters
@OneToMany(fetch = FetchType.LAZY, mappedBy = "feed")
public Set<FeedOrderDetail> getFeedOrderDetail() {
return this.feedOrderDetail ;
}
}
FeedOrderDetail.java
@Entity
public class FeedOrderDetail {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private double unitPrice;
private long quantity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FEED_ID", nullable = false)
public Feed getFeed() {
return this.feed;
}
//Getters and Setters
}
I hope it helps :)
Upvotes: 0
Reputation: 481
The correct one is:
@Entity
public class Feed {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
private long quantity;
//With getters and setters
@OneToMany(cascade=CascadeType.MERGE, mappedBy="feed")
private List<FeedOrderDetail> orders = new ArrayList();
}
And
@Entity
public class FeedOrderDetail {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private double unitPrice;
private long quantity;
@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="feed_id")
private Feed feed;
//Getters and Setters
}
Upvotes: 0
Reputation: 691735
You designed it backwards. You schema says that 1 Feed instance includes M FeedOrderDetail instances.
So, in the class Feed, you should have a List<FeedOrderDetail>
. But that's not what you've done. You have a List<Feed>
in FeedOrderDetail.
Upvotes: 1