无法坚持具有一对多关系的子条目
Can't persist child entry with one to many relationship
我有 3 个实体,一个是用户:
@Entity
@Table(name = "Users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "schoolId", referencedColumnName = "id", nullable = false)
private School school;
@Column(name = "emailAddress", nullable = false, unique=true)
private String emailAddress;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "pwd", nullable = false)
private String password;
@Column(name = "role", nullable = false)
private int role;
@ManyToMany(fetch=FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinTable(name = "Enrollment", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = {
@JoinColumn(name = "courseId") })
private Set<Course> courses;
@OneToMany(fetch=FetchType.EAGER, mappedBy = "user", cascade = { CascadeType.ALL })
private Set<Post> posts;
...
一个是课程:
@Entity
@Table(name = "Courses")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "schoolId", referencedColumnName = "id", nullable = false)
private School school;
@Column(name = "code", nullable = false)
private String code;
@Column(name = "name", nullable = false)
private String name;
@ManyToMany(fetch=FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinTable(name = "Enrollment", joinColumns = { @JoinColumn(name = "courseId") }, inverseJoinColumns = {
@JoinColumn(name = "userId") })
private Set<User> users;
@OneToMany(fetch=FetchType.EAGER, mappedBy = "course", cascade = { CascadeType.ALL })
private Set<Post> posts;
...
和Post:
@Entity
@Table(name = "Posts")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "courseId", referencedColumnName = "id", nullable = false)
private Course course;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "userId", referencedColumnName = "id", nullable = false)
private User user;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "content")
private String content;
@Column(name = "views", nullable = false)
private int views;
@Column(name = "importance", nullable = false)
private int importance;
@Column(name = "createdAt", nullable = false)
private Date createdAt;
@Column(name = "updatedAt")
private Date updatedAt;
...
它们之间的关系是:
Post - n:1 -> 课程
Post - n:1 -> 用户
我想做的是在数据库中添加一个新的 post 并更新相关课程和用户条目。下面是我的代码:
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
User user = session.load(User.class, (Integer) this.session.get("userId"));
Course course = session.load(Course.class, (Integer) this.session.get("courseId"));
Post post = new Post();
post.setContent(content);
post.setCreatedAt(new Date());
post.setTitle(title);
post.setViews(0);
post.setImportance(0);
user.getPosts().add(post);
course.getPosts().add(post);
session.persist(user);
session.persist(course);
transaction.commit();
session.close();
sessionFactory.close();
当代码执行时,这是我得到的错误:
1:非空 属性 引用空值或瞬态值:com.qury.models.Post.course
2: org.hibernate.PropertyValueException: 非空 属性 引用空值或瞬态值:com.qury.models.Post.course
我哪里做错了?我是否也应该设置 post 实体的用户和课程属性?
很简单 - transient 值表示尚未成为数据库一部分的内容。这些通常是新创建的对象,就像您的 Post post = new Post();
因此,不要将这些瞬态对象添加到其他实体的列表中,只需添加其他实体 ,它们是从数据库返回的,因此不是瞬态的 到 Post 实体。
换句话说转这个:
user.getPosts().add(post);
course.getPosts().add(post);
session.persist(user);
session.persist(course);
进入这个:
post.setUser(user);
post.setCourse(course);
session.persist(post);
我有 3 个实体,一个是用户:
@Entity
@Table(name = "Users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "schoolId", referencedColumnName = "id", nullable = false)
private School school;
@Column(name = "emailAddress", nullable = false, unique=true)
private String emailAddress;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "pwd", nullable = false)
private String password;
@Column(name = "role", nullable = false)
private int role;
@ManyToMany(fetch=FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinTable(name = "Enrollment", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = {
@JoinColumn(name = "courseId") })
private Set<Course> courses;
@OneToMany(fetch=FetchType.EAGER, mappedBy = "user", cascade = { CascadeType.ALL })
private Set<Post> posts;
...
一个是课程:
@Entity
@Table(name = "Courses")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "schoolId", referencedColumnName = "id", nullable = false)
private School school;
@Column(name = "code", nullable = false)
private String code;
@Column(name = "name", nullable = false)
private String name;
@ManyToMany(fetch=FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinTable(name = "Enrollment", joinColumns = { @JoinColumn(name = "courseId") }, inverseJoinColumns = {
@JoinColumn(name = "userId") })
private Set<User> users;
@OneToMany(fetch=FetchType.EAGER, mappedBy = "course", cascade = { CascadeType.ALL })
private Set<Post> posts;
...
和Post:
@Entity
@Table(name = "Posts")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "courseId", referencedColumnName = "id", nullable = false)
private Course course;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "userId", referencedColumnName = "id", nullable = false)
private User user;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "content")
private String content;
@Column(name = "views", nullable = false)
private int views;
@Column(name = "importance", nullable = false)
private int importance;
@Column(name = "createdAt", nullable = false)
private Date createdAt;
@Column(name = "updatedAt")
private Date updatedAt;
...
它们之间的关系是:
Post - n:1 -> 课程
Post - n:1 -> 用户
我想做的是在数据库中添加一个新的 post 并更新相关课程和用户条目。下面是我的代码:
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
User user = session.load(User.class, (Integer) this.session.get("userId"));
Course course = session.load(Course.class, (Integer) this.session.get("courseId"));
Post post = new Post();
post.setContent(content);
post.setCreatedAt(new Date());
post.setTitle(title);
post.setViews(0);
post.setImportance(0);
user.getPosts().add(post);
course.getPosts().add(post);
session.persist(user);
session.persist(course);
transaction.commit();
session.close();
sessionFactory.close();
当代码执行时,这是我得到的错误:
1:非空 属性 引用空值或瞬态值:com.qury.models.Post.course
2: org.hibernate.PropertyValueException: 非空 属性 引用空值或瞬态值:com.qury.models.Post.course
我哪里做错了?我是否也应该设置 post 实体的用户和课程属性?
很简单 - transient 值表示尚未成为数据库一部分的内容。这些通常是新创建的对象,就像您的 Post post = new Post();
因此,不要将这些瞬态对象添加到其他实体的列表中,只需添加其他实体 ,它们是从数据库返回的,因此不是瞬态的 到 Post 实体。
换句话说转这个:
user.getPosts().add(post);
course.getPosts().add(post);
session.persist(user);
session.persist(course);
进入这个:
post.setUser(user);
post.setCourse(course);
session.persist(post);