attributeOverrides在Hibernate中的行为不符合预期

时间:2016-02-21 19:33:03

标签: hibernate

我不明白为什么hibernate没有为office地址插入记录并将office_address的数据设置为家庭地址

预期产出:

 userid | home_city_name | home_pin_code | home_state_name | home_street_name | office_city_name | office_pin_code | office_state_name | office_street_name | username
--------+----------------+---------------+-----------------+------------------+------------------+-----------------+-------------------+--------------------+---------
      1 | Mumbai      | 12345         | Maharashtra       | AG Link Road     | Bangalore       | 54321           | Karnataka           | Marathahalli     | Pranay
(1 row)

我得到的输出:

 userid | home_city_name | home_pin_code | home_state_name | home_street_name | office_city_name | office_pin_code | office_state_name | office_street_name | username
--------+----------------+---------------+-----------------+------------------+------------------+-----------------+-------------------+--------------------+---------
      1 | Bangalore      | 54321         | Karnataka       | Marathahalli     |                  |                 |                   |                    | Pranay
(1 row) 

以下是该计划的摘要:

    @Embeddable
    public class Address {
        @Column (name="STREET_NAME")
        private String street;
        @Column (name="STATE_NAME")
        private String state;
        @Column (name="CITY_NAME")
        private String city;
        @Column (name="PIN_CODE")
        private String pincode;
        public String getStreet() {
            return street;
        }

    @Entity
    public class UserDetails {
        @Id @GeneratedValue (strategy=GenerationType.AUTO)  // used to make attribute Primary key in DB, We are asking hibernate to generate this value automatically
        private int userId;
        private String userName;

        @AttributeOverrides({
            @AttributeOverride (name="street", column=@Column (name="HOME_STREET_NAME")),
            @AttributeOverride (name="state", column=@Column (name="HOME_STATE_NAME")),
            @AttributeOverride (name="city", column=@Column (name="HOME_CITY_NAME")),
            @AttributeOverride (name="pincode", column=@Column (name="HOME_PIN_CODE"))
        })
        @Embedded
        private Address homeAddress;


        @AttributeOverrides({
            @AttributeOverride (name="street", column=@Column (name="OFFICE_STREET_NAME")),
            @AttributeOverride (name="state", column=@Column (name="OFFICE_STATE_NAME")),
            @AttributeOverride (name="city", column=@Column (name="OFFICE_CITY_NAME")),
            @AttributeOverride (name="pincode", column=@Column (name="OFFICE_PIN_CODE"))
        })
        @Embedded
        private Address officeAddress;
        ...
}


public static void main(String[] args) {
        UserDetails user = new UserDetails();
        user.setUserName("Pranay");

        Address addr = new Address();
        addr.setCity("Mumbai");
        addr.setPincode("12345");
        addr.setState("Maharashtra");
        addr.setStreet("AG link road");
        user.setHomeAddress(addr);

        Address addr2 = new Address();
        addr.setCity("Bangalore");
        addr.setPincode("54321");
        addr.setState("Karnataka");
        addr.setStreet("Marathahalli");
        user.setOfficeAddress(addr2);
        ...

}

1 个答案:

答案 0 :(得分:0)

您是在addr实例中而不是在addr2中设置它(可能是复制粘贴错误)。