Strikegently
Strikegently

Reputation: 2441

JPA one-to-many without an entity

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;
}

Upvotes: 2

Views: 1700

Answers (3)

Strikegently
Strikegently

Reputation: 2441

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: JPA @ElementCollection List specify join column name

Upvotes: 2

fg78nc
fg78nc

Reputation: 5232

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;
    }

Upvotes: 5

Priyak Dey
Priyak Dey

Reputation: 1367

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

Upvotes: 0

Related Questions