gpt4 book ai didi

java - Hibernate @OneToOne Mapping 不加入专栏?

转载 作者:行者123 更新时间:2023-11-29 00:01:03 26 4
gpt4 key购买 nike

我是 Java Hibernate 的新手,使用 hibernate 注释从 MySQL DB 连接和获取列。我有两个表 Order_headers 表和 Branch 表。我有分支代码在Order_headers 表中我有branch_code,在分支表中我有branch_code、branch_name、branch_desc。我想将 Branch 详细信息表列与 order_headers 表结合起来。

Order_headers.class

@Entity
@Table(name = "order_headers")
public class Order_headers {

@Column(name = "merchant_code")
private int merchant_code;

@Column(name = "device_id")
private String device_id;

@Id @GeneratedValue
@Column(name = "bill_number")
private int bill_number;

@Column(name = "order_number")
private String order_number;

@Column(name = "order_value")
private double order_value;

@Column(name = "payment_type")
private String payment_type;

@Column(name = "payment_status")
private String payment_status;

@Column(name = "order_datetime")
private Timestamp order_datetime;

//format YYYY-mm-dd
@Column(name = "order_date")
private String order_date;

//format HH:mm:ss
@Column(name = "order_time")
private String order_time;

@Column(name = "sub_total")
private double sub_total;

@Column(name = "VAT")
private double VAT;

@Column(name = "grand_total")
private double grand_total;

@Column(name = "branch_code")
private int branch_code;

@OneToOne(mappedBy = "order_headers", cascade=CascadeType.ALL)
@Fetch(FetchMode.JOIN)
private Branch branch;

@Column(name = "discount")
private double discount;

//Getter and setters

}

分支机构

@Entity
@Table(name = "branch")
public class Branch {

@Id @GeneratedValue
@Column(name = "branch_code")
private int branch_code;

@Column(name = "branch_name")
private String branch_name;

@OneToOne
@JoinColumn(name = "branch_code")
private Order_headers order_headers;

//Getter and setters

}

获取查询

ArrayList<Order_headers> order_details = (ArrayList<Order_headers>) session.createQuery("FROM Order_headers").list();

它只显示来自order_headers 表的数据。无法从Branch 表获取数据。帮我解决这个问题。

最佳答案

父实体中的 mappedBy 应引用您拥有父关联的子项的属性。使父级中的 mappedBy 属性更改如下。

@OneToOne(mappedBy = "order_headers", cascade=CascadeType.ALL)
private Branch branch;

关于java - Hibernate @OneToOne Mapping 不加入专栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com