无法保持具有一对多关系的子项输入

问题描述 投票:0回答:1

我有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;
    ...

和帖子:

@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;
    ...

它们之间的关系是:

发布-n:1->课程

发布-n:1->用户

我想做的是在数据库中添加新帖子,并更新相关的课程和用户条目。下面是我的代码:

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:非null属性引用的是null或瞬时值:com.qury.models.Post.course

2:org.hibernate.PropertyValueException:not-null属性引用了null或瞬时值:com.qury.models.Post.course

我在哪里弄错了?我也应该设置帖子实体的用户和课程属性吗?

hibernate one-to-many many-to-one
1个回答
0
投票

很简单-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);
© www.soinside.com 2019 - 2024. All rights reserved.