JPA 无实体的一对多关系

huangapple go评论90阅读模式
英文:

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 = &quot;FORTUNE_COOKIE&quot;)
class FortuneCookie {
    @Id
    @Column(name = &quot;ID&quot;)
    Integer id;

    @Column(name = &quot;MESSAGE&quot;)
    String message;

    @OneToMany
    @JoinTable(name = &quot;LUCKY_NUMBERS&quot;, joinColumns = {@JoinColumn(name = &quot;COOKIE_ID&quot;)})
    List&lt;Integer&gt; 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 = &quot;FORTUNE_COOKIE&quot;)
    class FortuneCookie {
        @Id
        @Column(name = &quot;ID&quot;)
        Integer id;
    
        @Column(name = &quot;MESSAGE&quot;)
        String message;
    
        @ElementCollection
        @CollectionTable(name=&quot;LUCKY_NUMBERS_TABLE&quot;)
        @Column(name=&quot;LUCKY_NUMBERS&quot;)
        List&lt;Integer&gt; 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 = &quot;FORTUNE_COOKIE&quot;)
class FortuneCookie {
    @Id
    @Column(name = &quot;ID&quot;)
    Integer id;

    @Column(name = &quot;MESSAGE&quot;)
    String message;

	@ElementCollection
    @CollectionTable(
		name = &quot;LUCKY_NUMBERS&quot;,//table name to join on
		joinColumns = @JoinColumn(
            //join LUCKY_NUMBERS on COOKIE_ID = ID
            name = &quot;COOKIE_ID&quot;,
            referencedColumnName = &quot;ID&quot;
        )
    )
    @Column(name = &quot;LUCKY_NUMBER&quot;) //column you want in the list
    List&lt;Integer&gt; 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到实体的更多细节。

Hibernate#collections

英文:

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.

Hibernate#collections

huangapple
  • 本文由 发表于 2020年8月8日 05:11:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63309191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定