我有两个班级:
Employee.java
@Entity
@Table
public class Employee {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
private Department department;
public Employee() {}
public Employee(String name, Department department) {
this.name = name;
this.department = department;
}
public Employee(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", department="
+ department.getName() + "]";
}
}
Department.java
@Entity
@Table
public class Department {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy="department",cascade=CascadeType.ALL)
private List<Employee> employees = new ArrayList<Employee>();
public Department() {
super();
}
public Department(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
然后我尝试在主要方法中保存department
和employees
:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Department department = new Department("java");
//session.save(department); //throw org.hibernate.TransientObjectException if I comment the line
session.save(new Employee("Jakab Gipsz",department));
session.save(new Employee("Captain Nemo",department));
session.getTransaction().commit();
它没有用。它抛出了一个错误:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.learn.hibernate.Department
但如果我取消注释行session.save(department);
,一切正常。为什么?我已经设置了cascade=CascadeType.ALL
,因此应该自动保存department
而不会丢失任何错误。我错过了什么吗?
答案 0 :(得分:1)
您的级联位于Employee
。这意味着,您需要使用添加的Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Department department = new Department("java");
deparmtment.getEmployees().add(new Employee("Jakab Gipsz",department));
deparmtment.getEmployees().add(new Employee("Captain Nemo",department));
session.save(department);
session.getTransaction().commit();
实例保存var result: NSArray = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray
println("result: \(result)")
实例。员工将会得救。
var str = "[{\"person\": {\"name\":\"Dani\",\"age\":\"24\"}},{\"person\": {\"name\":\"ray\",\"age\":\"70\"}}]"
var data : NSData = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
var result: NSArray = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray
println("Response: \(result)")
println("Oth Object: \(result[0])")
答案 1 :(得分:1)
保存员工时,部门未保存,因为没有级联。如果需要,您需要在Employee类中添加级联。
@ManyToOne(cascade = CascadeType.save)
private Department department;
答案 2 :(得分:0)
这是因为您在字段初始值设定项中创建了Employee
,因此如果它没有被托管Employee
实例覆盖,Hibernate会将其视为瞬态并在刷新时抛出异常。
解决方案不是在字段初始值设定项中创建它:
@OneToMany(mappedBy="department",cascade=CascadeType.ALL)
private List<Employee> employees;
您还必须关注 James 回答
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Department department = new Department("java");
deparmtment.getEmployees().add(new Employee("Jakab Gipsz",department));
deparmtment.getEmployees().add(new Employee("Captain Nemo",department));
session.save(department);
session.getTransaction().commit();