Problem in table name during creation by Hibernate

Asked

Viewed 63 times

0

I am "announcing" an entity Menu using Hibernate/jpa, and Menu contains an entity ShareActivity embeddeble that I created.

@Entity(name="menu")
public class Menu implements Component { 
    @Embedded
    @AssociationOverride(name="menu_share_activity",joinTable=@JoinTable(name="share_activity"))
    private ShareActivity enable;
    ...
    getters e setters
}

Component interface is empty

@Embeddable
public class ShareActivity {

    @NotNull
    @CollectionTable(name = "share_activity")
    private Map<Actor, Boolean> status;

    public ShareActivity() {
        this.status = new HashMap<Actor, Boolean>();
        this.status.put(Actor.owner, true);
        this.status.put(Actor.company, false);
        this.status.put(Actor.custumers, false);
        this.status.put(Actor.employee, false);
        this.status.put(Actor.friend, false);
    }
    ...
    getters e setters
}

I use an enumerator to restrict the number of actors in my map

public enum Actor {
    owner,
    friend,
    custumers,
    company,
    employee
}

At last when I add the variable ShareActivity in my class Menu it creates a table Menu_status, only that Menu is "noted" as @Entity(name="menu") and yet the table is still about creating the class name Menu capital letters, I’ve tried @Table(name="menu"), and it didn’t work.

Someone knows if you can do it, and if you know how you do it?

Kind of the whole database is under the pattern snake_case

1 answer

0


Use the annotation @AssociationOverride. Example:

Class Menu:

@Entity
public class Menu { 

    @Embedded
    @AssociationOverride(name="status",joinTable=@JoinTable(name="menu_share_activity"))
    private ShareActivity enable;

    //outros métodos e atributos 
}

Class ShareActivity:

@Embeddable
public class ShareActivity {

    @ElementCollection
    @MapKeyColumn(name="actor")
    @MapKeyEnumerated(EnumType.STRING)
    @Column(name="value")
    private Map<Actor, Boolean> status;

    public ShareActivity() {
        this.status = new HashMap<Actor, Boolean>();
        this.status.put(Actor.owner, true);
        this.status.put(Actor.company, false);
        this.status.put(Actor.custumers, false);
        this.status.put(Actor.employee, false);
        this.status.put(Actor.friend, false);
    }
}
  • thus creates only one table, and impossíbilita the reuse in other classes (I forgot to mention the map in B is annotated as Elementcollection and it by default names the table as variable classe_variable, and as it is Embedebla this elementCollection should do a_s) since A is with Entity(name="a")

  • @bruno_arabica I edited the answer with a solution for this case.

  • am receiving this error: org.hibernate.Mappingexception: Could not determine type for: java.util.Map, at table: A, for Columns: [org.hibernate.Mapping.Column(s)]. and the @Collectiontable annotation is not creating the table

  • @bruno_arabica you will have to edit your answer and put the corresponding classes A, B, C and D of your project. Without knowing how they are, it is not possible to help you.

  • Edited! The classes are javabeans, because I removed the other attributes and the methods put they are not so important to the question

  • @bruno_arabica I edited the answer with the solution.

  • perfectly faked, grateful for all the help

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.