从数据库加载用户并初始化该记录后,休眠状态将覆盖我加载的用户的数据,为什么会这样?如何停止这种更新行为?
用户dao:
public NormalUser findOne(Integer id) {
Session session = SessionHandler.getInstance().handle("open");
session.beginTransaction();
session.flush();
NormalUser user = session.load(NormalUser.class, id);
Hibernate.initialize(user);
session.getTransaction().commit();
session.close();
return user;
}
控制台日志:
Hibernate: select normaluser0_.ID as ID1_4_0_, normaluser0_.CREDENTIALS as CREDENTI2_4_0_, normaluser0_.BORN as BORN1_2_0_, normaluser0_.EMAIL as EMAIL2_2_0_, normaluser0_.FIRST_NAME as FIRST_NA3_2_0_, normaluser0_.LAST_NAME as LAST_NAM4_2_0_, normaluser0_.workspace_ID as workspac5_2_0_, normalwork1_.ID as ID1_5_1_, normalwork1_.ACCESS_TOKEN as ACCESS_T2_5_1_, normalwork1_.BALANCE_FK as BALANCE_3_5_1_, normalwork1_.CURRENCY as CURRENCY1_3_1_, balance2_.ID as ID1_0_2_, balance2_.WORKSPACE_FK as WORKSPAC2_0_2_ from NORMAL_USER normaluser0_ left outer join NormalWorkspace normalwork1_ on normaluser0_.workspace_ID=normalwork1_.ID left outer join BALANCE balance2_ on normalwork1_.BALANCE_FK=balance2_.ID where normaluser0_.ID=?
Hibernate: update NORMAL_USER set CREDENTIALS=?, BORN=?, EMAIL=?, FIRST_NAME=?, LAST_NAME=?, workspace_ID=? where ID=?
Hibernate: select normaluser0_.ID as ID1_4_0_, normaluser0_.CREDENTIALS as CREDENTI2_4_0_, normaluser0_.BORN as BORN1_2_0_, normaluser0_.EMAIL as EMAIL2_2_0_, normaluser0_.FIRST_NAME as FIRST_NA3_2_0_, normaluser0_.LAST_NAME as LAST_NAM4_2_0_, normaluser0_.workspace_ID as workspac5_2_0_, normalwork1_.ID as ID1_5_1_, normalwork1_.ACCESS_TOKEN as ACCESS_T2_5_1_, normalwork1_.BALANCE_FK as BALANCE_3_5_1_, normalwork1_.CURRENCY as CURRENCY1_3_1_, balance2_.ID as ID1_0_2_, balance2_.WORKSPACE_FK as WORKSPAC2_0_2_ from NORMAL_USER normaluser0_ left outer join NormalWorkspace normalwork1_ on normaluser0_.workspace_ID=normalwork1_.ID left outer join BALANCE balance2_ on normalwork1_.BALANCE_FK=balance2_.ID where normaluser0_.ID=?
Hibernate: update NORMAL_USER set CREDENTIALS=?, BORN=?, EMAIL=?, FIRST_NAME=?, LAST_NAME=?, workspace_ID=? where ID=?
编辑:根据要求,这里是实体。
NormalUser
扩展了抽象User
User
实体
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
@Column(name = "CREDENTIALS")
@Convert(converter = CredentialsConverter.class)
private Credentials credentials;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Credentials getCredentials() {
return credentials;
}
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
@Override
public String toString() {
return "User [id=" + id + ", credentials=" + credentials + "]";
}
}
Workspace
实体
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Workspace {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
@Column(name = "ACCESS_TOKEN")
private String accessToken;
@OneToMany(mappedBy = "workspace", cascade = CascadeType.ALL, targetEntity = NormalUser.class)
private List<NormalUser> users;
@OneToOne
@JoinColumn(name = "BALANCE_FK")
private Balance balance;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Balance getBalance() {
return balance;
}
public void setBalance(Balance balance) {
this.balance = balance;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public List<NormalUser> getUsers() {
return users;
}
public void setUsers(List<NormalUser> users) {
this.users = users;
}
}
Balance
实体,代表结果和收入
@Entity(name = "BALANCE")
public class Balance {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
@OneToOne
@JoinColumn(name = "WORKSPACE_FK")
private Workspace workspace;
@OneToMany(mappedBy = "balance", cascade = CascadeType.ALL, targetEntity = Expenditure.class)
@Column(name = "EXPENDITURES")
private List<Expenditure> outcomes;
public List<Expenditure> getOutcomes() {
return outcomes;
}
public void setOutcomes(List<Expenditure> outcomes) {
this.outcomes = outcomes;
}
}
Expenditure
代表所有结果
@Entity(name = "EXPENDITURE")
public class Expenditure implements Comparable<Expenditure> {
private String description;
@Convert(converter = MonetaryAmountConverter.class)
private MonetaryAmount monetaryAmount;
private LocalDate date;
private ExpenditureType type;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToOne
private Balance balance;
public Expenditure() {
}
public Expenditure(String description, Money money, LocalDate date, ExpenditureType expenditureType) {
this.description = description;
this.monetaryAmount = money;
this.date = date;
this.type = expenditureType;
}
public static Expenditure createExpenditure(String description, Money money, LocalDate date,
ExpenditureType expenditureType) {
return GenericBuilder.of(Expenditure::new).with(Expenditure::setDate, date)
.with(Expenditure::setMonetaryAmount, money).with(Expenditure::setDescription, description)
.with(Expenditure::setType, expenditureType).build();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Balance getBalance() {
return balance;
}
public void setBalance(Balance balance) {
this.balance = balance;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public MonetaryAmount getMonetaryAmount() {
return monetaryAmount;
}
public void setMonetaryAmount(MonetaryAmount monetaryAmount) {
this.monetaryAmount = monetaryAmount;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ExpenditureType getType() {
return type;
}
public void setType(ExpenditureType type) {
this.type = type;
}
@Override
public int compareTo(Expenditure o) {
return this.date.compareTo(o.date) + this.monetaryAmount.compareTo(o.monetaryAmount);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Expenditure)) {
return false;
}
Expenditure objToCompare = (Expenditure) obj;
if (this.getDate().compareTo(objToCompare.getDate()) == 0
&& this.getDescription().compareTo(objToCompare.getDescription()) == 0
&& this.getMonetaryAmount().compareTo(objToCompare.getMonetaryAmount()) == 0
&& this.getType().compareTo(objToCompare.getType()) == 0) {
return true;
}
return false;
}
}