Hibernate keeps creating tables for my enums

Viewed 260

I have two differents enums that are in two differents tables (they have the same structure, but different meanings).

This is my first table called TYPE, from FIRST_SCHEMA

id   team   desc              days
1    "AB"   "Do Thins"        2
2    "CD"   "Other things"    3    

And this is the second one, called TYPE, from SECOND_SCHEMA.

id   team   desc                       days
1    "AB"   "Other other thins"        1
2    "CD"   "More and more things"     1

I've tried to map this enum in Java using hibernate annotations and they are like this:

FirstEnum.java

@Entity
@Table(name = "TYPE", schema="FIRST_SCHEMA")
public enum FirstEnum implements SuperEnum<FirstEnum> {

    DO_THINGS(1, "AB", "Do things", 2),
    OTHER_THINGS(2, "CD", "Other things", 3);


    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private final int value;

    @Column(name = "team")
    private final String team;

    @Column(name = "desc")
    private final String dec;

    @Column(name = "days")
    private final int days;

    //getters and setters
}

SecondEnum.java

@Entity
@Table(name = "TYPE", schema="SECOND_SCHEMA")
public enum SecondEnum implements SuperEnum<SecondEnum> {

    DO_THINGS(1, "AB", "Other other thins", 2),
    OTHER_THINGS(2, "CD", "More and more thingsS", 3);


    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private final int value;

    @Column(name = "team")
    private final String team;

    @Column(name = "desc")
    private final String dec;

    @Column(name = "days")
    private final int days;

    //getters and setters
}

And the class which uses one of these types is mapped like this:

Foo.java

@Table(name = "TABLE", schema="FIRST_SCHEMA")
@Entity
public class Foo extends Bar implements Serializable {
    private static final long serialVersionUID = 1L;


    @Enumerated(EnumType.ORDINAL)
    @Column(name = "id_type")
    private FirstEnum type;

    //getters and setters
}

If I set in my persistence.xml the line:

<property name="hibernate.hbm2ddl.auto" value="update" />

to

<property name="hibernate.hbm2ddl.auto" value="none" />

It works as intended, it does not create a new "type" table. But, if not, it will create this empty table even thou I already have my types tables.

What am i mapping wrong?

1 Answers

First of all, enum is used for constant value.. If you put it in the table like that, might as well just create a class..

@Entity 
@Data 
@Accessors(chain = true) 
public class FirstEnum {


    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int value;

    @Column(name = "team")
    private String team;

    @Column(name = "desc")
    private String dec;

    @Column(name = "days")
    private int days;

}

The enum functionality is for having constant business logic in your class.. So maybe you can define the enum without having database instead..

public enum SecondEnum {

    DO_THINGS(1, "AB", "Other other thins", 2),
    OTHER_THINGS(2, "CD", "More and more thingsS", 3);
}
Related