Hibernate:如何在 Annotation 的一个连接表中加入三个 3 表?

编程入门 行业动态 更新时间:2024-10-26 12:29:53
本文介绍了Hibernate:如何在 Annotation 的一个连接表中加入三个 3 表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你能帮我如何用一张普通的连接表连接三个表吗?我有 USER、APPLICATION 和 ROLE 表.我希望他们的 ID 加入一个名为 USER_APP_ROLE(user.ID, application.ID, role.ID) 的表中.

Can you please help me how to join three tables with one common join table? I have USER, APPLICATION, and ROLE tables. And I want thier IDs to be joined in a table named USER_APP_ROLE(user.ID, application.ID, role.ID).

当我在多对多连接中删除应用程序或角色表时,我的代码正在运行.

When I remove either Application or Role tables in Join Many to Many my code is working.

我已经完成了以下代码:

I have done the following codes:

用户.java

@ManyToMany (targetEntity=Role.class) @JoinTable(name="USER_APPLICATION_ROLE", joinColumns=@JoinColumn(name="USER_ID"), inverseJoinColumns=@JoinColumn(name="ROLE_ID")) private Collection<Role> roles; @ManyToMany (targetEntity=Application.class) @JoinTable(name="USER_APPLICATION_ROLE", joinColumns=@JoinColumn(name="USER_ID"), inverseJoinColumns=@JoinColumn(name="APPLICATION_ID")) private Collection<Application> applications;

角色.java

@ManyToMany(mappedBy="roles", targetEntity=User.class) private Collection<User> users = new ArrayList<User>();

应用程序.java

@ManyToMany(mappedBy="applications", targetEntity=User.class) private Collection<User> users = new ArrayList<User>();

当我尝试运行以下测试时:

When I tried to run the following test:

user.getRoles().add(role1); user.getRoles().add(role2); role1.getUsers().add(user); role1.getUsers().add(user); role2.getUsers().add(user); role2.getUsers().add(user); user.getApplications().add(app1); user.getApplications().add(app2); app1.getUsers().add(user); app2.getUsers().add(user); ...... session.beginTransaction(); session.save(user); session.save(role1); session.save(role2); session.save(app1); session.save(app2);

我收到以下错误:

Hibernate: select seq_cm_user.nextval from dual Hibernate: select seq_role.nextval from dual Hibernate: select seq_role.nextval from dual Hibernate: select seq_application.nextval from dual Hibernate: select seq_application.nextval from dual Hibernate: insert into CM_USER (EMAIL, FIRST_NAME, LAST_NAME, MIDDLE_NAME, USERNAME, ID) values (?, ?, ?, ?, ?, ?) Hibernate: insert into CM_ROLE (DESCRIPTION, ID) values (?, ?) Hibernate: insert into CM_ROLE (DESCRIPTION, ID) values (?, ?) Hibernate: insert into CM_APPLICATION (CODE, DESCRIPTION, ID) values (?, ?, ?) Hibernate: insert into CM_APPLICATION (CODE, DESCRIPTION, ID) values (?, ?, ?) Hibernate: insert into USER_APPLICATION_ROLE (USER_ID, APPLICATION_ID) values (?, ?) Hibernate: insert into USER_APPLICATION_ROLE (USER_ID, APPLICATION_ID) values (?, ?) Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114) at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109) at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244) at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1179) at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) at org.hibernate.transaction.JDBCTransactionmit(JDBCTransaction.java:133) at com.hp.gdas.capman.HibernateTest.main(HibernateTest.java:73) Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("SYSTEM"."USER_APPLICATION_ROLE"."ROLE_ID") at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343) at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10657) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ... 14 more

非常感谢!

推荐答案

问题是您定义了与角色的关系和与应用程序的单独关系.没有声明任何特定用户是一个角色和一个应用程序的加入.

The problem is that you have defined a relationship to roles and a separate relationship to applications. There's no declaration that any specific user is a join of both one role and one application.

当您尝试与角色关联时,应用程序为空,反之亦然.

When you try to associate with a role, the application is null and visa-versa.

以下是使用 JPA 在 A、B 和 C 类之间映射三向连接的示例.注意,连接表不需要实体.

Here's an example of mapping a 3-way join between classes A, B and C using JPA. Note, no entity is required for the join table.

@Entity public class A { @JoinTable(name = "a_b_c", joinColumns = @JoinColumn(name = "a_id"), inverseJoinColumns = @JoinColumn(name = "c_id")) @MapKeyJoinColumn(name = "b_id") @ElementCollection private Map<B, C> cByB = new HashMap<>(); } @Entity public class B { ... } @Entity public class C { ... }

更多推荐

Hibernate:如何在 Annotation 的一个连接表中加入三个 3 表?

本文发布于:2023-10-17 06:21:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Hibernate   Annotation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!