gpt4 book ai didi

java - 如何坚持涉及@JoinFormula 的逆@OneToOne 映射?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:23 29 4
gpt4 key购买 nike

我正在尝试在 Hibernate 中映射以下表之间的关系:

create table binary (
id number not null primary key,
data blob,
entity_class varchar(255) not null,
entity_id number not null,
unique (entity_id, entity_class)
);

create table container_entity (
id number not null primary key,
...
);

二进制表应该保存任意其他表的二进制数据,“外键” - 虽然不是数据库术语 - 由 binary.entity_classbinary.entity_id< 组成。这是我现在必须接受的结构,它似乎在这里引起了困惑。 binary.entity_id 列引用聚合表的主键,而 binary.entity_class 定义聚合表本身:

BINARY                               CONTAINER_ENTITY_A  CONTAINER_ENTITY_B 
id entity_class entity_id id id ...
------------------------------- ------------------ ------------------
1 ContainerEntityA 1 -> 1 ...
2 ContainerEntityB 1 -> 1
3 ContainerEntityB 2 -> 2

当以只读方式使用时,ContainerEntity 中的映射已经可以找到:

@Entity @Table(name="container_entity_a")
public class ContainerEntityA {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@OneToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(column =
@JoinColumn(name = "id", referencedColumnName = "entity_id",
insertable=false, updatable=false)),
@JoinColumnOrFormula(formula =
@JoinFormula(value = "'ContainerEntityA'", referencedColumnName = "entity_class"))
})
private Binary binary;

public void setBinary(Binary aBinary) {
aBinary.setEntityClass("ContainerEntityA");
this.binary = aBinary;
}
}

@Entity @Table(name="binary")
public class Binary {
@Column(name = "entity_id", nullable = false)
private Long entityId;

@Column(name = "entity_class", nullable = false)
private String entityClass;
}

但是我在持久化 ContainerEntity 时遇到了问题:

  • 如果我只指定 CascadeType.PERSIST,Hibernate 无法设置 binary.entity_id
  • 如果我不使用 cascade-persist,我不知道什么时候自己设置 binary.entity_id,如何持久化映射对象,我最终得到:

    org.hibernate.TransientObjectException:对象引用未保存的 transient 实例 - 在刷新之前保存 transient 实例:ContainerEntity.binary -> Binary

换句话说,我希望但目前无法像这样持久化这两个实体:

containerEntity = new ContainerEntity();
containerEntity.setBinary( new Binary() );
entityManager.persist(containerEntity);

有什么想法或有用的建议吗?


赏金注意事项:这个问题还没有我可以接受为“正确”的答案,尽管还有一个提示我会在下周检查。不过,我的赏金时间已经结束,所以我会将它奖励给目前最接近的答案。

最佳答案

好的,请尝试以下我认为完美的方法。我已经测试并且可以按预期加载和保存容器和关联实体。

首先,容器必须从一些通用实体扩展:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Container {

//cannot use identity here however a table or sequence should work so long as
//the initial value is > current max ids from all container tables.
@Id
@TableGenerator(initialValue = 10000, allocationSize = 100, table = "id_gen", name = "id_gen")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "id_gen")
private Long id;

public Long getId() {
return id;
}

public BinaryData getBinaryData() {
return getData().size() > 0 ? getData().get(0) : null;
}

public void setBinaryData(BinaryData binaryData) {
binaryData.setContainerClass(getName());
binaryData.setContainer(this);

this.getData().clear();
this.getData().add(binaryData);
}

protected abstract List<BinaryData> getData();

protected abstract String getName();
}

混凝土容器A.该关系必须映射为 OneToMany,但是附加的 @Where 子句(以及您的数据库唯一键)有效地使其成为 @OneToOne。此类的客户可以将其视为单端关联:

@Entity
@Table(name = "container_a")
public class ContainerA extends Container {

@OneToMany(mappedBy = "container", cascade = CascadeType.ALL)
@Where(clause = "container_class = 'container_a'")
private List<BinaryData> binaryData;

public ContainerA() {
binaryData = new ArrayList<>();
}

@Override
protected List<BinaryData> getData() {
return binaryData;
}

@Override
protected String getName() {
return "container_a";
}
}

容器B

@Entity
@Table(name = "container_b")
public class ContainerB extends Container {

@OneToMany(mappedBy = "container", cascade = CascadeType.ALL)
@Where(clause = "container_class = 'container_b'")
private List<BinaryData> binaryData;

public ContainerB() {
binaryData = new ArrayList<>();
}

@Override
protected List<BinaryData> getData() {
return binaryData;
}

@Override
protected String getName() {
return "container_b";
}
}

从 BinaryData 映射回 Container 需要使用 Hibernate 的 @Any 映射。

@Entity
@Table(name = "binary_data")
public class BinaryData {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

@OneToOne
@Any(metaColumn = @Column(name = "container_class"))
@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
@MetaValue(targetEntity = ContainerA.class, value = "container_a"),
@MetaValue(targetEntity = ContainerB.class, value = "container_b") })
@JoinColumn(name = "entity_id")
private Container container;

@Column(name = "container_class")
private String containerClass;

public Long getId() {
return id;
}

public Container getContainer() {
return container;
}

public void setContainer(Container container) {
this.container = container;
}

public String getContainerClass() {
return containerClass;
}

public void setContainerClass(String containerClass) {
this.containerClass = containerClass;
}
}

以下测试按预期通过:

public class ContainerDaoTest extends BaseDaoTest {

@Test
public void testSaveEntityA() {

ContainerA c = new ContainerA();

BinaryData b = new BinaryData();
c.setBinaryData(b);

ContainerDao dao = new ContainerDao();
dao.persist(c);

c = dao.load(c.getId());
Assert.assertEquals(c.getId(), b.getContainer().getId());
}

@Test
public void testLoadEntity() {
ContainerA c = new ContainerDao().load(2l);
Assert.assertEquals(new Long(3), c.getBinaryData().getId());
Assert.assertEquals(new Long(2), c.getBinaryData().getContainer().getId());
Assert.assertEquals("container_a", c.getBinaryData().getContainerClass());
}

@Override
protected String[] getDataSetPaths() {
return new String[] { "/stack/container.xml", "/stack/binarydata.xml" };
}
}

使用以下数据集时:

<dataset>
<container_a id="1" />
<container_a id="2" />
<container_b id="1" />
<container_b id="2" />
</dataset>

<dataset>
<binary_data id="1" container_class="container_a" entity_id="1" />
<binary_data id="2" container_class="container_b" entity_id="2" />
<binary_data id="3" container_class="container_a" entity_id="2" />
<binary_data id="4" container_class="container_b" entity_id="1" />
</dataset>

关于java - 如何坚持涉及@JoinFormula 的逆@OneToOne 映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19818025/

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