如何使用许多一对多关系在hibernate中添加数据

时间:2014-11-22 17:22:09

标签: java hibernate

我有一个Song课程,与其他3个课程有一对多的关系 - 专辑,流派,登录。 这些是我的POJO课程:

   @Entity  
   @Table(name = "songs")  
   public class Song implements Serializable {  
    @Id  
    @GeneratedValue  
    @Column(name = "song_id")  
    private int songId;  

    @OneToOne(cascade = CascadeType.ALL)  
    @JoinTable(name = "albums_songs_mapping", joinColumns = @JoinColumn(name = "song_id", 
    referencedColumnName = "song_id"), inverseJoinColumns = @JoinColumn(name = 
    "album_id", referencedColumnName = "album_id"))  
    private Album album;  

    @Column(name = "title", nullable = false)  
    private String title;  

    @Column(name = "rating")  
    private int rating;  

    @Column(name = "artists")  
    private String artist;  

    @Column(name = "composer")  
    private String composer;  

    @OneToOne(cascade = CascadeType.ALL)  
    @JoinTable(name = "genre_songs_mapping", joinColumns = @JoinColumn(name = "song_id", 
    referencedColumnName = "song_id"), inverseJoinColumns = @JoinColumn(name =  
    "genre_id", referencedColumnName = "genre_id"))  
    private Genre genre;  
}  

@Entity  
@Table(name = "albums")  
public class Album {  

    @Id  
    @GeneratedValue  
    @Column(name = "album_id")  
    private int albumId;  

    @Column(name = "album_name", length = 250)  
    private String albumName;  

    @OneToMany(cascade=CascadeType.ALL)   
    @JoinTable(name = "albums_songs_mapping", joinColumns = @JoinColumn(name = 
    "album_id", referencedColumnName = "album_id"), inverseJoinColumns = @JoinColumn(name 
    = "song_id", referencedColumnName = "song_id"))  
    private List<Song> songs;  
}  


@Entity  
@Table(name = "genre")  
public class Genre {  

    @Id  
    @GeneratedValue  
    @Column(name = "genre_id")  
    private int genreId;  

    @Column(name = "genre_name", length = 250)  
    private String genreName;  

    @OneToMany(cascade=CascadeType.ALL)   
    @JoinTable(name = "genre_songs_mapping", joinColumns = @JoinColumn(name = "genre_id", 
    referencedColumnName = "genre_id"), inverseJoinColumns = @JoinColumn(name = 
    "song_id",referencedColumnName = "song_id"))  
    private List<Song> songs;  
}  

@Entity  
@Table(name = "login", uniqueConstraints = {@UniqueConstraint(columnNames = 
{"user_name"})})  
public class Login {  

    @Id  
    @GeneratedValue  
    @Column(name = "user_id")  
    private int userId;  

    @Column(name = "user_name", nullable=false )  
    private String userName;  

    @Column(name = "password", nullable=false )  
    private String password;  

    @ManyToMany  
    @JoinTable(name = "user_songs_mapping", joinColumns = @JoinColumn(name = " user_id", 
    referencedColumnName = "user_id"), inverseJoinColumns = @JoinColumn(name = "song_id", 
    referencedColumnName = "song_id"))  
    private List<Song> songs;  
}  

我在我的服务中尝试过这种方法。但它没有用。也没有任何错误。请帮我。

public void addSong(Song song, int userId) throws Exception {  

        Session session = HibernateUtil.getSessionFactory().openSession();  

        try {  

            session.beginTransaction();  
            List<Song> songs = new ArrayList<Song>();  
            songs.add(song);  
            Genre genre = song.getGenre();  
            Album album = song.getAlbum();  
            genre.setSongs(songs);  
            album.setSongs(songs);  
            Login login = new Login();  
            login.setUserId(userId);  
            login.setSongs(songs);  
            session.saveOrUpdate(genre);  
            session.saveOrUpdate(album);  
            session.saveOrUpdate(login);  

            session.getTransaction().commit();  
        } catch (HibernateException e) {  
            if (session.getTransaction() != null) {  
                session.getTransaction().rollback();  
            }  
        } finally {  
            session.close();  
        }  


    }  

1 个答案:

答案 0 :(得分:0)

您的Login实体具有不可为空的userNamepassword属性,但您在创建和保存Login实例时未设置它们,可能是你在评论中提到MysqlConstraintViolationException的原因。

要修复它,只需在保存实体之前设置用户名和密码。

希望这会有所帮助。如果没有,请发布异常的整个堆栈跟踪,以便我们可以更好地确定代码中的确切失败。