从数据库中获取后,JPA不会连接实体

时间:2016-01-20 17:59:58

标签: java database hibernate jpa

我正在生成5个实体类,但我只会展示2个有意义的东西。

UserPersonal

@Entity
@Table(name = "MP_USER_PERSONAL")
public class UserPersonal implements Serializable {

    private static final long serialVersionUID = 5681474541014032737L;

    private String firstName;
    private String lastName;
    private Collection<UserPersonalPhoneNumber> phoneNumbers;
    private User user;
    private Collection<Address> addresses;

    public UserPersonal() {
        phoneNumbers = new ArrayList<>();
    }

    public UserPersonal(String firstName, String lastName, User user) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.user = user;
    }

    @Column(name = "FIRST_NAME")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "LAST_NAME")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @OneToMany(mappedBy = "userPersonal", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Collection<UserPersonalPhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(Collection<UserPersonalPhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

    @Id
    @OneToOne
    @JoinColumn(name = "EMAIL")
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @OneToMany(mappedBy = "userPersonal", cascade = CascadeType.ALL)
    public Collection<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(Collection<Address> addresses) {
        this.addresses = addresses;
    }

    @Override
    public String toString() {
        return "UserPersonal [firstName=" + firstName + ", lastName=" + lastName + ", phoneNumbers=" + phoneNumbers
                + ", addresses =" + addresses + "]";
    }

}

ADDRESS

@Entity
@Table(name = "MP_USER_PERSONAL_ADDRESS", uniqueConstraints = {
        @UniqueConstraint(columnNames = {"EMAIL", "CITY", "STREET", "HOUSE"})
})
public class Address implements Serializable {

    private static final long serialVersionUID = 1309109354717562031L;

    private long id;
    private String city;
    private String street;
    private String house;
    private UserPersonal userPersonal;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, name = "ID")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "CITY")
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Column(name = "STREET")
    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Column(name = "HOUSE")
    public String getHouse() {
        return house;
    }

    public void setHouse(String house) {
        this.house = house;
    }

    @ManyToOne
    @JoinColumn(name = "EMAIL")
    public UserPersonal getUserPersonal() {
        return userPersonal;
    }

    public void setUserPersonal(UserPersonal userPersonal) {
        this.userPersonal = userPersonal;
    }

    @Override
    public String toString() {
        return "Address [id=" + id + ", city=" + city + ", street=" + street + ", house=" + house + "]";
    }

}

之后我尝试通过适当的方法

获取条目
public void findUser() {
            UserPersonal user = em.find(UserPersonal.class, "3");
            logger.info("{}", user.toString());
            logger.info("{}", user.getAddresses().size());
        }

查看控制台,有一个选择,即hibernate对数据库执行请求并获得正确的结果,它看起来像:

mysql> select userperson0_.EMAIL as EMAIL3_1_0_, userperson0_.FIRST_NAME as FIRST_NA1_1_0_, userperson0_.LAST_NAME as LAST_NAM2_1_0_, addresses1_.EMAIL as EMAIL5_2_1_, addresses1_.ID as ID1_2_1_, addresses1_.ID as ID1_2_2_, addresses1_.CITY as CITY2_2_2_, addresses1_.HOUSE as HOUSE3_2_2_, addresses1_.STREET as STREET4_2_2_, addresses1_.EMAIL as EMAIL5_2_2_ from MP_USER_PERSONAL userperson0_ left outer join MP_USER_PERSONAL_ADDRESS addresses1_ on userperson0_.EMAIL=addresses1_.EMAIL where userperson0_.EMAIL='3';
+-------------+----------------+----------------+-------------+----------+----------+------------+-------------+--------------+-------------+
| EMAIL3_1_0_ | FIRST_NA1_1_0_ | LAST_NAM2_1_0_ | EMAIL5_2_1_ | ID1_2_1_ | ID1_2_2_ | CITY2_2_2_ | HOUSE3_2_2_ | STREET4_2_2_ | EMAIL5_2_2_ |
+-------------+----------------+----------------+-------------+----------+----------+------------+-------------+--------------+-------------+
| 3           | vova           | black          | 3           |        4 |        4 | kiev       | 10          | mira         | 3           |
+-------------+----------------+----------------+-------------+----------+----------+------------+-------------+--------------+-------------+
1 row in set (0,00 sec)

最后记录器返回此结果,其中地址不存在且size = 0:

UserPersonal [firstName=vova, lastName=black, phoneNumbers=[], addresses =[]]

0

为什么呢?哪里出错?

0 个答案:

没有答案