英文:
JPA one-to-many without an entity
问题
在JPA中,是否可以在不创建实体的情况下建立一对多的关系?
例如,假设我有一个“幸运饼干”,其中包含几个“幸运号码”。这些幸运号码存储在一个只有[cookie_id,lucky_number]的表中。
在JPA中,是否可以获取幸运号码的列表,而无需为其创建专用实体?
这是我尝试的方式,但它给我一个错误,提示“使用@OneToMany或@ManyToMany目标到一个未映射的类”。
@Entity
@Table(name = "FORTUNE_COOKIE")
class FortuneCookie {
@Id
@Column(name = "ID")
Integer id;
@Column(name = "MESSAGE")
String message;
@OneToMany
@JoinTable(name = "LUCKY_NUMBERS", joinColumns = {@JoinColumn(name = "COOKIE_ID")})
List<Integer> luckyNumbers;
}
希望这可以帮助你理解如何在JPA中处理这种情况。
英文:
In JPA, is it possible to have a one-to-many relationship without creating an entity?
As an example, say I have a Fortune Cookie that has several "lucky numbers." These lucky numbers are stored in a table that is only [cookie_id, lucky_number].
In JPA, is it possible to get a list of the lucky numbers without having to create a dedicated entity for it?
This is what I tried, but it gives me an error for Use of @OneToMany or @ManyToMany targeting an unmapped class
@Entity
@Table(name = "FORTUNE_COOKIE")
class FortuneCookie {
@Id
@Column(name = "ID")
Integer id;
@Column(name = "MESSAGE")
String message;
@OneToMany
@JoinTable(name = "LUCKY_NUMBERS", joinColumns = {@JoinColumn(name = "COOKIE_ID")})
List<Integer> luckyNumbers;
}
答案1
得分: 3
您可以使用 @ElementCollection
注解来声明多值映射。集合的记录将存储在单独的表中,您可以使用 @CollectionTable
注解来定义该表。
@Entity
@Table(name = "FORTUNE_COOKIE")
class FortuneCookie {
@Id
@Column(name = "ID")
Integer id;
@Column(name = "MESSAGE")
String message;
@ElementCollection
@CollectionTable(name="LUCKY_NUMBERS_TABLE")
@Column(name="LUCKY_NUMBERS")
List<Integer> luckyNumbers;
}
英文:
You can use @ElementCollection
annotation to declare an multivalued mapping. The records of the collection will be stored in a separate table, which you can define with the @CollectionTable
annotation
@Entity
@Table(name = "FORTUNE_COOKIE")
class FortuneCookie {
@Id
@Column(name = "ID")
Integer id;
@Column(name = "MESSAGE")
String message;
@ElementCollection
@CollectionTable(name="LUCKY_NUMBERS_TABLE")
@Column(name="LUCKY_NUMBERS")
List<Integer> luckyNumbers;
}
答案2
得分: 1
以下是问题的解决方案。+1 给 @fg78nc,他引导我走上了正确的道路。
@Entity
@Table(name = "FORTUNE_COOKIE")
class FortuneCookie {
@Id
@Column(name = "ID")
Integer id;
@Column(name = "MESSAGE")
String message;
@ElementCollection
@CollectionTable(
name = "LUCKY_NUMBERS", // 要加入的表名
joinColumns = @JoinColumn(
// 将 LUCKY_NUMBERS 表连接到 COOKIE_ID = ID
name = "COOKIE_ID",
referencedColumnName = "ID"
)
)
@Column(name = "LUCKY_NUMBER") // 你想要的列表中的列名
List<Integer> luckyNumbers;
}
来源:https://stackoverflow.com/questions/22075199/jpa-elementcollection-list-specify-join-column-name
英文:
Here's the solution to the problem. +1 to @fg78nc who got me on the right track.
@Entity
@Table(name = "FORTUNE_COOKIE")
class FortuneCookie {
@Id
@Column(name = "ID")
Integer id;
@Column(name = "MESSAGE")
String message;
@ElementCollection
@CollectionTable(
name = "LUCKY_NUMBERS",//table name to join on
joinColumns = @JoinColumn(
//join LUCKY_NUMBERS on COOKIE_ID = ID
name = "COOKIE_ID",
referencedColumnName = "ID"
)
)
@Column(name = "LUCKY_NUMBER") //column you want in the list
List<Integer> luckyNumbers;
}
Adapted from: https://stackoverflow.com/questions/22075199/jpa-elementcollection-list-specify-join-column-name
答案3
得分: 0
你可以加入表格,这些表格在JPA领域中被表示为实体。因此,当你使用注解如OneToMany或ManyToMany时,JPA期望与另一个实体建立关系。在这里,你定义了一个整数列表,这不是一个实体。所以要像这样定义一个关系,你可以使用@ElementCollection。这将创建另一个表格,具有相同的一对多关系,使用默认表格名称,你还可以使用其他一些注解进行自定义。
由于你没有提到你使用的JPA提供程序,我假设是Hibernate。
请参考下面的文档,获取关于如何映射List/Set的String/Integer到实体的更多细节。
英文:
You can join tables, which are represented as entities in the JPA domain. so when you use annotations like - OneToMany or ManyToMany, JPA is expecting a relationship with another entity. here you have defined a list of Integer, which is not an entity. So define a relationship like this, you can use @ElementCollection. This creates another table with the same one to many relationship with default table names, which again can be customized using some other annotations.
Since you have not mentioned the JPA provider you are using, I am assuming hibernate.
PLease refer the below docs to get more details about mapping of List/Set of String/Integer...... within a n entity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论