0

Inside MySQL i created some tables. I have a table user who contains all the items. But now i want to make an estimate who has a list of items. from the tbl_items.

So i MySql i did something like this:

enter image description here

But now if i have trouble with the mapping of the list items:

This is my estimate class :

@Entity
@Table(name = "tbl_estimate")
@SecondaryTable(name = "tbl_order_items", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "fk_id_estimate_order", referencedColumnName = "id_estimate")})
public class Estimate {

    //Table estimate
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_estimate",table = "tbl_estimate")
    private Integer id ;

    @Column(table = "tbl_estimate")
    private Integer fkIdUserEstimate;

    @Column(table = "tbl_estimate")
    private Integer fkIdClientEstimate;


    //table order items
    @Column(table = "tbl_order_items")
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "fk_id_estimate_order", cascade = CascadeType.ALL)
    @Fetch(FetchMode.JOIN)
    private List<Item> itemList = new ArrayList<>();


....

Can someone give me some directions on this ?

1 Answer 1

2

First off, when using JPA you should FORGET about the design of your database when designing entities. Mapping foreign key id's is neither necessary or something one should do. JPA is all about object orientation and hiding the details of the database design.

If you want a OneToMany from Estimate to Item, the simplest way of doing this is through a JoinColumn:

@Entity
@Table(name = "tbl_estimate")
public class Estimate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_estimate")
    private Integer id ;

    @JoinColumn(name = "estimate_id")
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Item> items = new ArrayList<>();     

    ...
}

Then your item-table needs a foreign column to the estimate table named estimate_id. Alternatively, use a join table:

@Entity
@Table(name = "tbl_estimate")
public class Estimate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_estimate")
    private Integer id ;

    @JoinTable(name = "estimate_item")
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Item> items = new ArrayList<>();     

    ...
}

This will require a table named estimate_item, with columns estimate_id and items_id, each with a foreign key to one of the tables. It is possible to override the names of the columns with nested a @JoinColumn with inverseJoinColumn in the @JoinTable annotation.

When working with JPA, it's best to start with the design of the entities, and create the tables based on this. This approach will usually give the cleanest design.

Also, you use the mappedBy-attribute wrong, it's value is not the foreign key in the database, but rather the variable name on the other side of the entity relationship.

Sign up to request clarification or add additional context in comments.

1 Comment

Very good explanation of what i'm doing wrong. I got the joincolum working. But now i'm going to try your example with JoinTable. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.