本文介绍了Hibernate:如何级联插入OnetoMany子代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试持久化一个新的‘UserTopics’对象,并在与多个UserID对应的‘Theme’表中映射新的UserTheme。

我不知道我做错了什么。下面是我的代码和例外情况。

我的用户主题实体:

@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

在我的服务类中,我尝试按如下方式保存用户主题:

 @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)

推荐答案

所以解决方案是:

将级联类型添加到UserTopics实体中的Topics对象,如下所示:

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

而My 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);
    }

这篇关于Hibernate:如何级联插入OnetoMany子代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 08:44