簡體   English   中英

如何將一個實體映射到兩個表?

[英]How do I map one entity to two tables?

主鍵必須映射到兩個表; 第一張桌子是多對一,第二張是一對一; 我怎么做? 我給你我的課

package salepoint.model;

/*i've deleted imports*/

@Entity
@Table (name = "sales")
@SecondaryTables({
    @SecondaryTable(name = "sold_products", pkJoinColumns = {
        @PrimaryKeyJoinColumn(name = "sales_id")})})
public class Sale implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @ManyToOne
    @JoinTable(name = "user_sales", joinColumns = @JoinColumn(name =                         "sale_id"))
private Integer id;

@Column (name = "amount")
private Double amount;

@Column (name = "date")
private Date date;

@Column (name = "user_id")
private Integer user_id;

public Sale() {
}

public Sale(Double amount, Date date, Integer user_id) {
    this.amount = amount;
    this.date = date;
    this.user_id = user_id;
}

public Sale(Integer id, Double amount, Date date, Integer user_id) {
    this.id = id;
    this.amount = amount;
    this.date = date;
    this.user_id = user_id;
}

@Override
public String toString() {
    return "Sale{"
            + "id = " + id
            +", amount = " + amount
            + ", date = " + date
            + ", user_id = " + user_id + '}';
}
/*i've deleted getters and setters, stack overflow didn't want me to put it*/
}

希望你能幫助我,伙計們; 我很絕望

請糾正我我在這里錯了,

您正在嘗試映射用戶及其銷售表。 那么你為什么需要在銷售表中創建多對一映射它應該在用戶 pojo 中

list<sale> sales;

上面的屬性應該是多對一的注釋。 在銷售 pojo 中,

User user;

用一對多注釋就可以了。

你看起來像這樣嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM