我有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。对不起,如果我很糟糕的错误。
谢谢。
你是什么意思'应用程序的另一层' – hvgotcodes 2011-02-07 01:33:33
@hvgotcodes:“* Begin *”和“* End *”之间的代码片段在应用程序的另一层中执行,例如,在另一个服务器或Web服务中执行,我不能在那里访问会话。我恢复了我想坚持解组的对象。只是想记下发生的事情的时间流程,并不意味着真实的代码就是这样,这些变量的名字被选择来显示我想要做的事情。 – Bene 2011-02-07 09:12:26