0

I have the following setup

table: table1
| id | name |
|  1 | test |

table: table2
| id | table1_id | value |
| 1  |    1      | ENUM1 |
| 2  |    1      | ENUM2 |

And then, I have the following Java code:

@Entity
public class Table1 {

    @Id
    @GeneratedValue
    private Long id;

    @NotEmpty
    @Column(unique = true)
    private String name;

    @OneToMany
    @JoinTable(
        name = "TABLE2",
        joinColumns = @JoinColumn(name = "table1_id")
    )
    @Enumerated(EnumType.STRING)
    private Set<TheEnum> enumValues;

}

But, when I am trying to run my Spring boot app I get the following error:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: ${package}.Table1.enumValues

Do you know what I have to do?

Manuelarte
  • 1,658
  • 2
  • 28
  • 47
  • 1
    Answer is in [stackoverflow question 15998824](http://stackoverflow.com/questions/15998824/mapping-setenum-using-elementcollection) Bye, Hans – Hans Poo Mar 29 '17 at 12:30

1 Answers1

3

@Enumerated says whether TheEnum object is stored as int or string in database.

You want to map collection enumValues to join table TABLE2. Use this code:

@ElementCollection(targetClass = TheEnum.class)
@JoinTable(name = "TABLE2", joinColumns = @JoinColumn(name = "table1_id"))
@Column(name = "enumValues", nullable = false)
@Enumerated(EnumType.STRING)
Set<TheEnum> enumValues;
Jay Smith
  • 2,331
  • 3
  • 16
  • 27