如何将一个实体映射到两个表?

时间:2018-03-05 10:00:59

标签: java hibernate

主键必须映射到两个表;在第一张桌子上,我得到了多对一,第二张是一对一;我怎么做?我给你我的课

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*/
}

希望你能帮助我,伙计们;我很绝望

1 个答案:

答案 0 :(得分:1)

请纠正我,我错了,

您正在尝试映射用户及其销售表。那你为什么需要在sales表中创建多对一映射,它应该在用户pojo中用

创建
list<sale> sales;

上面的属性应该用多对一注释。并在销售pojo,

User user;

用一对多注释即可。

你看起来像这样吗?

相关问题