Hibernate:如何通过级联插入OnetoMany子项

时间:2017-01-22 19:03:56

标签: hibernate one-to-many hibernate-cascade

我正在尝试保留一个新的'UserTopics'对象,并将新的UserTopic映射到对应多个userId的'Topic'表中。

我不知道我在这里做错了什么。下面是我的代码和例外。

我的UserTopics实体:

@Entity
@Table(name="USERS_TOPICS")
public class UserTopics {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="TOPICUSER_ID")
    private Integer id;

    @Column(name="USER_ID")
    private Integer userId;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "TOPICS_TOPICS_ID")
    private Topics topics;

    // Getters and setters

和主题实体:

    @Entity
    @Table(name="TOPICS")
    public class Topics {

        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        @Column(name="TOPICS_ID")
        private Integer id;

        @Column(name="TOPICNAME")
        private String topicName;

        @OneToMany(mappedBy = "topics", cascade= {CascadeType.ALL,CascadeType.PERSIST})
        private Set<UserTopics> userTopics;

       //Getter and setters

在我的服务类中,我试图像这样保存UserTopic:

 @Service("userTopicsService")
    @Transactional
    public class UserTopicsServiceImpl implements UserTopicsService {


        @Autowired
        TopicsDao topicsDao;


        @Override
        public void createTopicc(int UserIdOne, int UserIdTwo) {
        Set<UserTopics> userTopics = new HashSet<>();

        Topics topic = new Topics();
        topic.setTopicName(String.valueOf(UserIdOne+UserIdTwo));

        UserTopics userTopicOne = new UserTopics();
        userTopicOne.setUserId(UserIdOne);
        userTopics.add(userTopicOne);

        UserTopics userTopicTwo = new UserTopics();
        userTopicTwo.setUserId(UserIdTwo);
        userTopics.add(userTopicTwo);

        topic.setUserTopics(userTopics);

        topicsDao.saveTopic(topic);

    }

   //Other methods...

以下例外

    18:58:54.434 [http-apr-8080-exec-9] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1048, SQLState: 23000
18:58:54.434 [http-apr-8080-exec-9] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Column 'TOPICS_TOPICS_ID' cannot be null
18:58:54.442 [http-apr-8080-exec-9] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Warning
java.sql.SQLWarning: Column 'TOPICS_TOPICS_ID' cannot be null
    at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:779)
    at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:707)

2 个答案:

答案 0 :(得分:0)

对于每个UserTopic,您应该在保存之前设置Topic对象:

public void createTopicc(int UserIdOne, int UserIdTwo) {
    Set<UserTopics> userTopics = new HashSet<>();

    Topics topic = new Topics();
    topic.setTopicName(String.valueOf(UserIdOne+UserIdTwo));

    UserTopics userTopicOne = new UserTopics();
    userTopicOne.setUserId(UserIdOne);
    userTopicOne.setTopics(topic);
    userTopics.add(userTopicOne);

    UserTopics userTopicTwo = new UserTopics();
    userTopicTwo.setUserId(UserIdTwo);
    userTopicTwo.setTopics(topic);
    userTopics.add(userTopicTwo);

    topic.setUserTopics(userTopics);

    topicsDao.saveTopic(topic);

<强>更新

此外,在您的主题实体中..使用hibernate cascade选项,如follwoing(而不是JPA):

@OneToMany(mappedBy = "topics")
@Cascade({CascadeType.SAVE_UPDATE})
private Set<UserTopics> userTopics;

答案 1 :(得分:0)

所以解决方案是:

在UserTopics实体中添加级联类型到Topics对象:

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "TOPICS_TOPICS_ID")
    private Topics topics;

虽然我的Service类中保存新主题及其子项UserTopics的代码如下所示。

    Topics topic = new Topics();
    topic.setTopicName(String.valueOf(UserIdOne+UserIdTwo));

    Set<UserTopics> userTopics = new HashSet<>();

    UserTopics userTopicOne = new UserTopics();
    userTopicOne.setUserId(UserIdOne);
    userTopicOne.setTopics(topic);

    UserTopics userTopicTwo = new UserTopics();
    userTopicTwo.setUserId(UserIdTwo);
    userTopicOne.setTopics(topic);

    userTopics.add(userTopicOne);
    userTopics.add(userTopicTwo);

    List<UserTopics> userTopicsList = new ArrayList<>();
    userTopics.add(userTopicOne);
    userTopics.add(userTopicTwo);

    for(UserTopics next : userTopics) {
        userTopicsDao.save(next);
    }