Java 将主键作为外键映射到另一个表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20180933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mapping of primary key as foreign key to another table
提问by nkr
I have a problem regarding a foreign key relationship and its JPA counterpart.
我有一个关于外键关系及其 JPA 对应关系的问题。
In the following tables. means_of_transport
is a table with a consecutive numbering. The other tables have less entries with a mapping of their primary key as a foreign key to the primary key of means_of_transport
. Sadly, I cannot change the data model.
在下表中。means_of_transport
是一个连续编号的表格。其他表的条目较少,它们的主键作为外键映射到 的主键means_of_transport
。遗憾的是,我无法更改数据模型。
Table means_of_transport
桌子 means_of_transport
CREATE TABLE means_of_transport (
id INT PRIMARY KEY NOT NULL,
max_capacity INT
);
Table ocean_ship
桌子 ocean_ship
CREATE TABLE ocean_ship (
means_of_transport_id INT PRIMARY KEY NOT NULL,
name VARCHAR(45) NOT NULL,
FOREIGN KEY ( means_of_transport_id ) REFERENCES means_of_transport ( id )
);
CREATE INDEX fk_ship_means_of_transport1_idx ON ocean_ship ( means_of_transport_id );
There are more tables besides ocean_ship
with the same key structure but other fields.
除了ocean_ship
具有相同键结构但其他字段之外,还有更多表。
MeansOfTransport.java
MeansOfTransport.java
@Entity
@Table(name = "means_of_transport")
public class MeansOfTransport extends Model {
@Id
public Integer id;
public Integer maxCapacity;
}
OceanShip.java
海洋之船.java
@Entity
@Table(name = "ocean_ship")
public class OceanShip extends Model {
@Id
@OneToOne
@JoinColumn(table = "means_of_transport", referencedColumnName = "id")
public MeansOfTransport meansOfTransport;
public String name;
}
Now every time I try to use OceanShip
in another JPA class as a field, I get this error: Error reading annotations for models.Classname
.
现在,每次我尝试OceanShip
在另一个 JPA 类中将其用作字段时,都会收到此错误:Error reading annotations for models.Classname
.
Am I missing some annotations?
我是否缺少一些注释?
edit
编辑
Example usage:
用法示例:
@ManyToOne
@Column(name = "ocean_ship")
public OceanShip oceanShip;
Full stack trace:
完整的堆栈跟踪:
play.api.UnexpectedException: Unexpected exception[RuntimeException: Error reading annotations for models.ContainerOrder]
at play.core.ReloadableApplication$$anonfun$get$$anonfun$apply$$anonfun.apply(ApplicationProvider.scala:150) ~[play_2.10.jar:2.1.5]
at play.core.ReloadableApplication$$anonfun$get$$anonfun$apply$$anonfun.apply(ApplicationProvider.scala:114) ~[play_2.10.jar:2.1.5]
at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
at play.core.ReloadableApplication$$anonfun$get$$anonfun$apply.apply(ApplicationProvider.scala:114) ~[play_2.10.jar:2.1.5]
at play.core.ReloadableApplication$$anonfun$get$$anonfun$apply.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.1.5]
at scala.util.Either$RightProjection.flatMap(Either.scala:523) ~[scala-library.jar:na]
Caused by: java.lang.RuntimeException: Error reading annotations for models.ContainerOrder
at com.avaje.ebeaninternal.server.deploy.parse.ReadAnnotations.readAssociations(ReadAnnotations.java:54) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readDeployAssociations(BeanDescriptorManager.java:1034) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readEntityDeploymentAssociations(BeanDescriptorManager.java:565) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.deploy(BeanDescriptorManager.java:252) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.core.InternalConfiguration.<init>(InternalConfiguration.java:124) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:210) ~[avaje-ebeanorm-server.jar:na]
Caused by: java.lang.NullPointerException: null
at com.avaje.ebeaninternal.server.deploy.BeanTable.createJoinColumn(BeanTable.java:94) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.parse.AnnotationAssocOnes.readAssocOne(AnnotationAssocOnes.java:145) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.parse.AnnotationAssocOnes.parse(AnnotationAssocOnes.java:54) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.parse.ReadAnnotations.readAssociations(ReadAnnotations.java:45) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readDeployAssociations(BeanDescriptorManager.java:1034) ~[avaje-ebeanorm-server.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readEntityDeploymentAssociations(BeanDescriptorManager.java:565) ~[avaje-ebeanorm-server.jar:na]
采纳答案by nkr
The error message came up because some annotations were wrong.
出现错误消息是因为某些注释是错误的。
I changed the primary key of OceanShip
from MeansOfTransport
to Integer
and have now to be careful what I enter there. That's the price I have to pay.
我将OceanShip
from的主键更改为MeansOfTransport
to Integer
,现在必须小心我在那里输入的内容。这是我必须付出的代价。
Additionally I changed the annotation where I use OceanShip
from this
另外,我OceanShip
从这里更改了我使用的注释
@ManyToOne
@Column(name = "ocean_ship")
public OceanShip oceanShip;
to this
对此
@ManyToOne
@JoinColumn(name = "ocean_ship", referencedColumnName = "means_of_transport_id")
public OceanShip oceanCarrierShip;
回答by peter.petrov
I think your DB model is conceptually not right.
我认为您的数据库模型在概念上是不正确的。
It should be something like this below.
下面应该是这样的。
The ocean_ship_id is the PK of the ocean_ship table. And means_of_transport_id from ocean_ship is just a FK to the "base" table means_of_transport.
Ocean_ship_id 是 Ocean_ship 表的 PK。而来自ocean_ship 的means_of_transport_id 只是“基本”表means_of_transport 的FK。
So I would say you get your DB model right, then worry about the JPA mapping.
所以我会说你的数据库模型是正确的,然后担心 JPA 映射。
CREATE TABLE ocean_ship (
ocean_ship_id INT PRIMARY KEY NOT NULL,
means_of_transport_id INT NOT NULL,
name VARCHAR(45) NOT NULL,
FOREIGN KEY ( means_of_transport_id ) REFERENCES means_of_transport ( id )
);
回答by peter.petrov
See @PrimaryKeyJoinColumn
and the ideas given in this thread.
请参阅@PrimaryKeyJoinColumn
此线程中给出的想法。
Shared primary key with JPA @MapsId annotation
Might be helpful maybe?
可能有帮助吗?