gpt4 book ai didi

java - 修改注释以避免 `unique constraint or index violation` 错误?

转载 作者:行者123 更新时间:2023-11-30 11:25:34 26 4
gpt4 key购买 nike

我正尝试着手解决 hibernate 问题,但遇到了一些障碍。当我尝试将代码插入到我的 hsqldb

时,我得到了一个 SQLIntegrityConstraintViolationException

基本设置是有一个歌曲播放列表表。播放列表应该只是映射到它们包含的 song_id。所以,我将我的类(class)设置如下。

播放列表类

@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 相同,这就可以了。

enter image description here

正如您在图片中看到的,当事物不重叠时,一切都会保存 OK。但是,如果我尝试将已使用的 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)

我假设这是因为我对映射的理解不足。谁能帮我解决这个问题?

最佳答案

@OneToMany(fetch=FetchType.EAGER) @Cascade(CascadeType.ALL) 
private List<Song> songs;

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

关于java - 修改注释以避免 `unique constraint or index violation` 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20235415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com