我有 2 个这样的映射:
<hibernate-mapping>
<class name="A" table="A">
<id name="code" column="aCode" type="integer">
<generator class="assigned"/>
</id>
<property name="info" type="integer"/>
<set name="Bs" lazy="false">
<key>
<column name="aCode" not-null="true"/>
</key>
<one-to-many class="B"/>
</set>
</class>
<class name="B" table="B">
<id name="code" column="bCode" type="integer">
<generator class="assigned"/>
</id>
<many-to-one name="a"/>
</class>
</hibernate-mapping>
这些是类:
public class A {
private int code;
private int info;
private Set<B> bs = new HashSet<B>(0);
public A() {};
public int getCode() { return code; }
public void setCode(int code) { this.code = code; }
public int getInfo() { return info; }
public void setInfo(int info) { this.info = info; }
public Set<B> getBs() { return bs; }
public void setBs(Set<B> bs) { this.bs = bs; }
}
public class B {
private int code;
private A a;
public B() {};
public int getCode() { return code; }
public void setCode(int code) { this.code = code; }
public A getA() { return a; }
public void setA(A a) { this.a = a; }
}
我处于必须处理长期过渡并执行以下操作的情况:
// Persistence Layer
Session session = factory.getCurrentSession();
session.beginTransaction();
A a1 = new A(); // Create transient object
a1.setCode(1);
a1.setInfo(10);
session.save(a1); // Persist it
// Something happening in another layer (see below)
// Continuing with the transaction
Object b = ... // Recover object
session.save(b); // Persist it using transient a2 object as a link but don't change/update its data
System.out.println(b.getA().getInfo()); // Returns 0 not 10;
session.commit();
这发生在另一层(这里无权访问会话):
// * Begin in another layer of the application *
A a2 = new A(); // Create another transient object same *code* as before
a2.setCode(1);
B b = new B(); // Create another transient object
b.setCode(1);
b.set(a2);
// * End and send the b Object to the persistence layer *
在保存父对象之前是否有任何方法加载/获取持久子对象,或者是否有其他方法可以保存子对象而不更改信息并将其全部刷新?我没有使用 JPA。对不起,如果我大错特错。
谢谢你。