Not-updatable child entity
I have such method:
@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(o开发者_运维技巧bject);
}
Entity User2 contains entity Person. As you see, first - I am adding a new record to a database and then I just want to update sonme fields of User2 and Person. The problem is that only User2 field are updated (HQL query is generated only for User2 entity too). How can I make it to update a Person entity too? Thank you
@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 */ }
Why are you ding it this way.Please try the following:
@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);
精彩评论