【问题标题】:Modifying Annotations to avoid `unique constraint or index violation` error?修改注释以避免“唯一约束或索引冲突”错误?
【发布时间】:2013-11-27 06:27:08
【问题描述】:

我正试图绕过休眠,但我遇到了一些障碍。当我尝试将代码插入我的 hsqldb 时,我收到了 SQLIntegrityConstraintViolationException

基本设置是有一个songplaylisttable。播放列表应该只是映射到它们包含的 song_ids。所以,我的课程设置如下。

播放列表类

@Entity
public class MyPlaylist {
    @Id @GeneratedValue
    private int id;
    private String name;
    @ElementCollection
    @OneToMany(fetch=FetchType.EAGER) @Cascade(CascadeType.ALL) 
    private List<Song> songs;

歌曲课

@Entity
public class Song {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String title;
    private String artist; 
    private String album; 
    private String filePath; 
    private String trackLength;

客户代码

// Along the lines of...
List<Song> playlistSongs = loadSongs();
Playlist p = new Playlist("MyPLaylist", playlistSongs);
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session s = factory.openSession();
s.beginTransaction();
s.saveOrUpdate(p);
s.close();

现在,只要没有两首歌曲具有相同的 id,这就可以了

正如您在图片中看到的那样,当事情不重叠时,一切都会保存好。但是,如果我尝试将已经使用的 song_id 转换为新的 playlist_id,事情就会爆炸并抛出:

Exception in thread "AWT-EventQueue-0" org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1256)
    at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:393)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:385)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:307)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1240)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at com.titanplayer.persistence.Database.save(Database.java:35)
    at com.titanplayer.gui.PlayerGUI.newPlaylistButtonActionPerformed(PlayerGUI.java:441)
    at com.titanplayer.gui.PlayerGUI.access$2200(PlayerGUI.java:47)
    at com.titanplayer.gui.PlayerGUI$8.actionPerformed(PlayerGUI.java:321)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; UK_Q3MYU734MY659N16AROSDTO8P table: PLAYLIST_SONG
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)

我假设这是由于我对映射的了解不足。谁能帮我解决这个问题?

【问题讨论】:

  • 为什么列表中不同歌曲的歌曲ID相同?有什么原因吗?

标签: java hibernate


【解决方案1】:
@OneToMany(fetch=FetchType.EAGER) @Cascade(CascadeType.ALL) 
private List<Song> songs;

我认为问题就在这里。你有歌曲数据库,对吧?用户可以创建播放列表并在其中填充任何歌曲集。所以你需要没有级联的@ManyToMany

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多