gpt4 book ai didi

hibernate - 同样,JoinTable 未填充多对多 hibernate 关系

转载 作者:行者123 更新时间:2023-12-03 06:55:17 27 4
gpt4 key购买 nike

|AA|多对多|BB|多对多 |CC|

与连接表映射成为

|AA|--|AA_BB|--|BB|--|BB_CC|--|CC|

(抱歉,我无法发布图片,声誉低)

编辑:简短的问题是为什么如果a创建Aa元素,Bb元素,Cc元素将 Cc 添加到 Bb 集合,将 Bb 添加到 Aa 集合保存(Aa)工作正常(AA_BB 已填充)?

但是如果我创建一个 Bb 元素、Aa 元素、Cc 元素将 Aa 添加到 Bb 集合中将抄送添加到 Bb 收藏保存(Bb)(AA_BB填写?)

BB_CC 始终已满。

结束编辑

 CREATE TABLE BB_CC (
BBIDBB number(10) NOT NULL,
CCIDCC number(10) NOT NULL,
PRIMARY KEY (BBIDBB,
CCIDCC));
CREATE TABLE AA_BB (
AAIDAA number(10) NOT NULL,
BBIDBB number(10) NOT NULL,
PRIMARY KEY (AAIDAA,
BBIDBB));
CREATE TABLE CC (
IDCC number(10) NOT NULL,
DESCR varchar2(10) NOT NULL,
PRIMARY KEY (IDCC));
CREATE TABLE BB (
IDBB number(10) NOT NULL,
DESCR varchar2(10) NOT NULL,
PRIMARY KEY (IDBB));
CREATE TABLE AA (
IDAA number(10) NOT NULL,
DESCR varchar2(10) NOT NULL,
PRIMARY KEY (IDAA));
ALTER TABLE BB_CC ADD CONSTRAINT FKBB_CC976918 FOREIGN KEY (BBIDBB) REFERENCES BB (IDBB);
ALTER TABLE BB_CC ADD CONSTRAINT FKBB_CC529716 FOREIGN KEY (CCIDCC) REFERENCES CC (IDCC);
ALTER TABLE AA_BB ADD CONSTRAINT FKAA_BB470776 FOREIGN KEY (AAIDAA) REFERENCES AA (IDAA);
ALTER TABLE AA_BB ADD CONSTRAINT FKAA_BB23574 FOREIGN KEY (BBIDBB) REFERENCES BB (IDBB);

hibernate 配置文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- ORACLE -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@10.10.1.8:1521:GEROS</property>
<property name="connection.username">xxxx</property>
<property name="connection.password">xxxx</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup-->
<property name="hbm2ddl.auto">create</property>

<!-- Names the annotated entity class -->
<mapping class="it.erreeffe.erreeffe2.Aa"/>
<mapping class="it.erreeffe.erreeffe2.Bb"/>
<mapping class="it.erreeffe.erreeffe2.Cc"/>
</session-factory>
</hibernate-configuration>

以及表中的反向 jpa 类Aa(Bb、Cc 类似)缺少一些部分。

@Entity
public class Aa implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name="AA_IDAA_GENERATOR", sequenceName="SEQ_AA")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AA_IDAA_GENERATOR")
private long idaa;

private String descr;

//bi-directional many-to-many association to Bb
@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(
name="AA_BB"
, joinColumns={
@JoinColumn(name="AAIDAA")
}
, inverseJoinColumns={
@JoinColumn(name="BBIDBB")
}
)
private Set<Bb> bbs;

我的测试,第一个有效,第二个无效。

第一:

Session session;
SessionFactory sf_ORA = new Configuration().configure(
"hibernate.cfg.ora.xml").buildSessionFactory();
session = sf_ORA.openSession();

//A
Aa a = new Aa();
a.setDescr("A1");
a.setBbs(new HashSet<Bb>());
//B
Bb b1 = new Bb();
b1.setCcs(new HashSet<Cc>());
Bb b2 =new Bb();
b2.setCcs(new HashSet<Cc>());
b1.setDescr("B1");
b2.setDescr("B2");
//C
Cc c1 = new Cc();
Cc c2 = new Cc();
Cc c3 = new Cc();
c1.setDescr("C1");
c2.setDescr("C2");
c3.setDescr("C3");
//FILL B
b1.getCcs().add(c1);
b1.getCcs().add(c2);
b2.getCcs().add(c2);
b2.getCcs().add(c3);
//FILL A
a.getBbs().add(b1);
a.getBbs().add(b2);

session.beginTransaction();
try
{
session.save(a);
session.flush();
session.getTransaction().commit();
}
catch(HibernateException ex)
{
session.getTransaction().rollback();
throw ex;
}

这做正确的工作:Hibernate 为我创建了所有需要的语句:

Hibernate: insert into Aa (descr, idaa) values (?, ?)
Hibernate: insert into Bb (descr, idbb) values (?, ?)
Hibernate: insert into Cc (descr, idcc) values (?, ?)
Hibernate: insert into Cc (descr, idcc) values (?, ?)
Hibernate: insert into Bb (descr, idbb) values (?, ?)
Hibernate: insert into Cc (descr, idcc) values (?, ?)
Hibernate: insert into AA_BB (AAIDAA, BBIDBB) values (?, ?)
Hibernate: insert into AA_BB (AAIDAA, BBIDBB) values (?, ?)
Hibernate: insert into BB_CC (BBIDBB, CCIDCC) values (?, ?)
Hibernate: insert into BB_CC (BBIDBB, CCIDCC) values (?, ?)
Hibernate: insert into BB_CC (BBIDBB, CCIDCC) values (?, ?)
Hibernate: insert into BB_CC (BBIDBB, CCIDCC) values (?, ?)

连接结果查询是正确的:

A1|B1|C1

A1|B1|C2

A1|B2|C2

A1|B2|C3

第二(Ta-dan 这就是问题所在)从DB读取B1,添加新的Aa(A2)到B1。Aas集合添加新的Cc(c4)到B1。Ccs集合,更新B1。

Bb b1=null;
session.beginTransaction();
try
{
Query qGetB1=session.createQuery("Select id from Bb where DESCR ='B1'" );
long idB1=(Long) qGetB1.list().get(0);
b1=(Bb) session.load(Bb.class, idB1);
session.flush();
session.getTransaction().commit();
}
catch(HibernateException ex)
{
session.getTransaction().rollback();
throw ex;
}

Aa a2 = new Aa();
a2.setDescr("A2");
Cc c4 = new Cc();
c4.setDescr("C4");

b1.getAas().add(a2);
b1.getCcs().add(c4);


session.beginTransaction();
try
{
session.flush();
session.saveOrUpdate(b1);
session.flush();
session.getTransaction().commit();
}
catch(HibernateException ex)
{
session.getTransaction().rollback();
throw ex;
}

生成 Hibernate:

Hibernate: select bb0_.idbb as col_0_0_ from Bb bb0_ where DESCR='B1' //SEARCH B1 ID
Hibernate: select bb0_.idbb as idbb1_0_, bb0_.descr as descr1_0_ from Bb bb0_ where bb0_.idbb=? //GET B1
Hibernate: select aas0_.BBIDBB as BBIDBB1_1_, aas0_.AAIDAA as AAIDAA1_, aa1_.idaa as idaa0_0_, aa1_.descr as descr0_0_ from AA_BB aas0_, Aa aa1_ where aas0_.AAIDAA=aa1_.idaa and aas0_.BBIDBB=? GET B1.Aas
Hibernate: select ccs0_.BBIDBB as BBIDBB1_1_, ccs0_.CCIDCC as CCIDCC1_, cc1_.idcc as idcc2_0_, cc1_.descr as descr2_0_ from BB_CC ccs0_, Cc cc1_ where ccs0_.CCIDCC=cc1_.idcc and ccs0_.BBIDBB=? GET B1.Ccs
Hibernate: insert into Aa (descr, idaa) values (?, ?) //OK
Hibernate: insert into Cc (descr, idcc) values (?, ?) //OK
Hibernate: insert into BB_CC (BBIDBB, CCIDCC) values (?, ?) //OK


... What about AA_BB record???? //!!!!!!!!

为什么joinTable AA_BB没有填满?

感谢大家的关注。问候,弗朗西斯科。

最佳答案

当您有双向关联时,一侧是所有者侧(不带 mappedBy 属性的一侧),另一侧是反向侧(具有 mappedBy 属性的一侧)。

Hibernate 仅考虑所有者方来了解两个实体之间是否存在关联。因此,如果 A 是 A 和 B 之间关系的所有者方,则必须将 B 实例添加到 A 的集合中才能使关联持久化。将 A 实例添加到 B 的集合中没有任何效果。

一般来说,负责维护对象图的一致性,因此应该确保所有者端(至少)始终更新。

关于hibernate - 同样,JoinTable 未填充多对多 hibernate 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9635551/

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