2017-01-22 99 views
1

我正在嘗試堅持一個新的'UserTopics'對象,並將'UserTopic'中的新UserTopic映射到與多個userId對應的'Topic'表中。休眠:如何通過級聯插入OnetoMany兒童

我不知道我在做什麼錯在這裏。以下是我的代碼和例外情況。

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

回答

0

因此,解決辦法是:

新增級聯類型主題的UserTopics實體對象爲這樣:

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

,而在我的服務類保存新的主題及其子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); 
    } 
0

對每一個UserTopic你應該設置主題對象,然後再儲存:

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級聯選項像follwoing(而不是一個JPA ):

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

我試過,但我得到同樣的錯誤(見下文)。 '插入 成 TOPIC_USERS (TOPICS_TOPICS_ID,USER_ID) 值 休眠(?,?): 插入 成 TOPIC_USERS (TOPICS_TOPICS_ID,USER_ID) 值 com.mysql.jdbc(?,?)。 exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'TOPICS_TOPICS_ID'不能爲空' – user2342259

+0

@ MaciejKowalski我正在考慮保存UserTopic,從會話中返回ID,然後調用persist並傳遞新的主題集合,我將設置它我剛創建的UserTopic ... – user2342259

+0

是的,但爲什麼你需要級聯的t母雞爲?我已經添加了一個可以檢查的datail。試試吧,讓我知道 –