我有这样的方法:
@Transactional
public void add(User2 object) {
Session session = SessionFactoryUtils.getSession(sessionFactory, false);
session.save(object);
String number = generateNumber(object.getPerson().getId(), object.getPerson().getFranchise().getId());
object.setLogin(number);
object.getPerson().setNumber(number);
session.update(object);
}
实体User2包含实体Person。如您所见,首先 - 我正在向数据库添加新记录,然后我只想更新User2和Person的sonme字段。问题是只更新User2字段(仅为User2实体生成HQL查询)。我怎样才能更新Person实体呢? 谢谢
@Entity @Table(name="users") public class User2 implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
@Id
@Column(name = "id")
@GeneratedValue
private Long id;
/**
*
*/
@Column(name = "login")
private String login;
/**
*
*/
@Column(name = "password")
private String password;
/**
*
*/
@OneToOne(cascade = CascadeType.ALL, targetEntity = Person2.class, orphanRemoval = true)
@JoinColumn(name = "person_id")
private Person2 person;
/**
*
*/
public User2() {
this.setId(0L);
} /*getters ans setters*/ }
@Entity @Table(name="persons") public class Person2 implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
@Id
@Column(name = "id")
@GeneratedValue
private Long id;
/**
*
*/
@Column(name = "title", nullable = true)
private String position;
/**
*
*/
@Column(name = "firstname", nullable = true)
private String firstName;
/**
*
*/
@Column(name = "lastname", nullable = true)
private String lastName;
/**
*
*/
@Column(name = "birthdate", nullable = true)
private String birthDate;
/**
*
*/
@Column(name = "Notes")
private String notes;
/**
*
*/
public Person2() {
this.setId(0L);
} /* getters ans setters */ }
答案 0 :(得分:1)
你为什么这样做。请尝试以下方法:
@Transactional
public void add(User2 object) {
Session session = SessionFactoryUtils.getSession(sessionFactory, false);
String number = generateNumber(object.getPerson().getId(), object.getPerson().getFranchise().getId());
object.setLogin(number);
object.getPerson().setNumber(number);
session.saveOrUpdate(object);