gpt4 book ai didi

java - 复合键类中的@OneToOne 注释不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 20:07:09 24 4
gpt4 key购买 nike

也许有人可以澄清下面的代码有什么问题。当我在嵌入式类中创建一对一关联时(它是复合主键),如下面的代码所示:

@Entity
public class Test {

@EmbeddedId
private TestId id;

@Embeddable
public static class TestId implements Serializable {
private static final long serialVersionUID = 1950072763330622759L;

@OneToOne(optional = false)
@JoinColumn(name = "linkedTable_id")
private LinkedTable linkedTable;

}
..........
}

我得到以下堆栈跟踪:

--------------------------------------------

Caused by: java.lang.NullPointerException
at org.hibernate.cfg.AnnotationBinder.bindOneToOne(AnnotationBinder.java:1867)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1286)
at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:1662)
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1695)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1171)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:706)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1211)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:847)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:178)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:235)
... 26 more

有趣的是,如果我将关联类型更改为多对一而不适用于一对一,为什么上面的示例可以正常工作?

最佳答案

我不知道这是可能的,但根据 Hibernate 注释引用文档,它是(尽管这是 Hibernate 特定的):

2.2.3.2.1. @EmbeddedId property

(...)

While not supported in JPA, Hibernate lets you place your association directly in the embedded id component (instead of having to use the @MapsId annotation).

@Entity
class Customer {
@EmbeddedId CustomerId id;
boolean preferredCustomer;
}

@Embeddable
class CustomerId implements Serializable {
@OneToOne
@JoinColumns({
@JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
@JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
})
User user;
String customerNumber;
}

@Entity
class User {
@EmbeddedId UserId id;
Integer age;
}

@Embeddable
class UserId implements Serializable {
String firstName;
String lastName;
}

使用您提供的代码,以下代码片段对我来说非常适用:

LinkedTable linkedTable = new LinkedTable();
linkedTable.setId(1l);
session.persist(linkedTable);
session.flush();

Test.TestId testId = new Test.TestId();
testId.setLinkedTable(linkedTable);
Test test = new Test();
test.setId(testId);
session.persist(test);
session.flush();

使用 Hibernate EM 3.4.0.GA、Hibernate Annotations 3.4.0.GA 和 Hibernate Core 3.3.0.SP1 进行测试。

如果它不适合你,你能提供更多的代码来重现问题吗?

关于java - 复合键类中的@OneToOne 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3266319/

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