Java-使用地址对象更新用户对象的两个复杂字段(办公室地址和家庭地址)

时间:2018-08-01 14:56:24

标签: java setter

我在和Hibernate和Postgres一起玩耍。 我有一个用户类别,如下所示

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name="USER_DETAILS")
public class UserDetails {

@Id @GeneratedValue
private int userId;
private String userName;
@Temporal(TemporalType.DATE)
private Date joinedDate;
@Embedded
private Address homeAddress;
@Embedded
@AttributeOverrides({
    @AttributeOverride(name="street", column = @Column(name="OFFICE_STREET_NAME")),
    @AttributeOverride(name="city", column = @Column(name="OFFICE_CITY_NAME")),
    @AttributeOverride(name="state", column = @Column(name="OFFICE_STATE_ABBR")),
    @AttributeOverride(name="pincode", column = @Column(name="OFFICE_PIN_CODE"))
})
private Address officeAddress;

@Lob
@Type(type="text")
private String description;

public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public Date getJoinedDate() {
    return joinedDate;
}
public void setJoinedDate(Date joinedDate) {
    this.joinedDate = joinedDate;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public Address getHomeAddress() {
    return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
    this.homeAddress = homeAddress;
}
public Address getOfficeAddress() {
    return officeAddress;
}
public void setOfficeAddress(Address officeAddress) {
    this.officeAddress = officeAddress;
}
}

和下面的地址类别

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {
@Column(name="STREET_NAME")
private String street;
@Column(name="CITY_NAME")
private String city;
@Column(name="STATE_NAME")
private String state;
@Column(name="PINCODE_NUMBER")
private String pincode;
public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getPincode() {
    return pincode;
}
public void setPincode(String pincode) {
    this.pincode = pincode;
}
}

主类如下所示

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.loganathan.divakar.dto.Address;
import org.loganathan.divakar.dto.UserDetails;

public class HibernateTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    UserDetails user = new UserDetails();
    user.setUserName("First User");
    user.setJoinedDate(new Date());
    user.setDescription("First User Description");

    Address addr = new Address();

    addr.setStreet("Home Street");
    addr.setCity("Home City");
    addr.setState("Home State Abbr");
    addr.setPincode("Home PinCode");
    user.setHomeAddress(addr);

    addr.setStreet("Office Street");
    addr.setCity("Office City");
    addr.setState("Office State");
    addr.setPincode("Office PinCode");
    user.setOfficeAddress(addr);

    SessionFactory sessionFactory = new 
    Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
    session.close();
}

}

所以现在当我运行Main类时,用户对象的HomeAddress和OfficeAddress都具有(OfficeAddress)的值,​​所以我有意使用了相同的地址对象“ addr”。

Address addr = new Address();

首先将值设置为带有家庭地址详细信息的addr对象,然后将addr对象设置为UserDetails对象(用户)的homeAddress字段

addr.setStreet("Home Street");
addr.setCity("Home City");
addr.setState("Home State Abbr");
addr.setPincode("Home PinCode");
user.setHomeAddress(addr);

然后将值设置为具有Office地址详细信息的addr对象,然后将addr对象设置为UserDetails对象(用户)的officeAddress字段

addr.setStreet("Office Street");
addr.setCity("Office City");
addr.setState("Office State");
addr.setPincode("Office PinCode");
user.setOfficeAddress(addr); 

如果我使用两个Address对象(MainClass中的homeAddr和offAddr对象)设置userDetails对象的homeAddress和OfficeAddress值,则此方法完全可以正常工作。

但是我想尝试使用一个Address对象为家庭和办公室地址设置不同的值。

我知道我们可以使用相同的地址对象从逻辑上为UserDetails类的家庭和办公室地址字段设置值。 但是我不明白,为什么homeAddress值被OfficeAddress值覆盖(即使在设置User.homeAddress字段之后,甚至在输入officeAddress值之前)

我什至使用调试模式进行调试,以查看为什么homeAddress值被OfficeAddress值覆盖。

非常感谢您提供任何有助于理解家庭地址值为何被Office地址值覆盖的信息。预先感谢。

关于, Divakar

1 个答案:

答案 0 :(得分:0)

发生这种情况是因为您仍在使用相同的地址对象。如果希望它们是单独的地址,则必须创建一个新地址。

Address addr = new Address();
addr.setStreet("Home Street");
addr.setCity("Home City");
addr.setState("Home State Abbr");
addr.setPincode("Home PinCode");
user.setHomeAddress(addr);

addr = new Address(); // make a new address
addr.setStreet("Office Street");
addr.setCity("Office City");
addr.setState("Office State");
addr.setPincode("Office PinCode");
user.setOfficeAddress(addr); 

将地址设置为家庭地址时,addr和用户的homeAddress引用相同的Address对象,并且对它的更改将在两个地方反映出来。