渴望加载相关实体

时间:2018-03-08 08:16:30

标签: java hibernate

我正在使用hibernate for ORM,我有实体User,Person和地址与OnetoOne关系我使用Eager fetchType。地址与人和人有关。当我从jsp输出时,我只从User获取值,而其他值为null。

用户

@Entity
@Table(name = "User1")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id", unique = true, nullable = false)
private int id;

@Column(name = "Username",length = 50)
private String username;

@Column(name = "Email",length = 50)
private String email;

@Column(name = "ContactNO" ,length = 50)
private String contact_no;

@Column(name = "Password", length = 50)
private String password;

@Transient
private boolean canEdit;

@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Person person;  

@Entity
@Table(name = "Person")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id", unique = true, nullable = false)
private int person_id;

@Column(name = "Name", length = 50)
private String name;

@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Address address;

@Transient
private boolean canEdit;

地址

@Entity
@Table(name="Address")
public class Address {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id", unique = true, nullable = false)
private int add_id;

@Column(name ="Street" ,length = 50)
private String street;

@Column(name = "City" ,length = 50)
private String city;

@Column(name = "PostalCode" ,length = 50)
private String postalCode;

@Column(name = "Country" ,length = 50)
private String country;

@Transient
private boolean canEdit;

1 个答案:

答案 0 :(得分:0)

要映射表之间的关系,您应指明两个表之间应连接的列。您可以使用在OneToOne anotation中放置的MappedBy属性来完成此操作。

你可以尝试一下这个:

用户实体

@Entity
@Table(name = "User1")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id", unique = true, nullable = false)
private int id;

@Column(name = "Username",length = 50)
private String username;

@Column(name = "Email",length = 50)
private String email;

@Column(name = "ContactNO" ,length = 50)
private String contact_no;

@Column(name = "Password", length = 50)
private String password;

@Transient
private boolean canEdit;

// add mappedBy to indicate person in table Person
@OneToOne(fetch = FetchType.EAGER, mappedBy = "user", cascade = CascadeType.ALL)
private Person person;  

人员实体

@Entity
@Table(name = "Person")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id", unique = true, nullable = false)
private int person_id;

@Column(name = "Name", length = 50)
private String name;

// For OneToOne relation between User-Person
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id") // assume you express this relation by adding user's id to Person
private User user; 

// add mappedBy to indicate person in table Address
@OneToOne(fetch = FetchType.EAGER, mappedBy = "person", cascade = CascadeType.ALL)
private Address address;

@Transient
private boolean canEdit;

地址实体

@Entity
@Table(name="Address")
public class Address {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Id", unique = true, nullable = false)
private int add_id;

@Column(name ="Street" ,length = 50)
private String street;

@Column(name = "City" ,length = 50)
private String city;

@Column(name = "PostalCode" ,length = 50)
private String postalCode;

@Column(name = "Country" ,length = 50)
private String country;

// For OneToOne relation between Person-Address
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "person_id") // assume you express this relation by adding person's id to Address
private Person person;

@Transient
private boolean canEdit;

此外,您还可以找到更多详细信息here