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
OceanShip.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
.
现在,每当我尝试在另一个JPA类中使用OceanShip作为字段时,我都会收到此错误:读取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$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:150) ~[play_2.10.jar:2.1.5]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.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$1$$anonfun$apply$1.apply(ApplicationProvider.scala:114) ~[play_2.10.jar:2.1.5]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.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]
3 个解决方案
#1
3
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 )
);
#2
1
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的主键从MeansOfTransport更改为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;
#3
1
See @PrimaryKeyJoinColumn
and the ideas given in this thread.
请参阅@PrimaryKeyJoinColumn以及此主题中给出的想法。
Shared primary key with JPA @MapsId annotation
与JPA @MapsId注释共享主键
Might be helpful maybe?
可能有帮助吗?
#1
3
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 )
);
#2
1
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的主键从MeansOfTransport更改为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;
#3
1
See @PrimaryKeyJoinColumn
and the ideas given in this thread.
请参阅@PrimaryKeyJoinColumn以及此主题中给出的想法。
Shared primary key with JPA @MapsId annotation
与JPA @MapsId注释共享主键
Might be helpful maybe?
可能有帮助吗?