Mamun
Mamun

Reputation: 375

Hibernate CollectionTable - How to set the name of mapped table's column name?

I am creating a @CollectionTable in a Spring RESTfull API. I have an Entity table named University. Now I am creating an Entity called TargetedGift where I will have a list of University's id which will be store in PostgreSQL database.

The TargetedGift Entity would like this:

@Data
@Entity
@Table(name = "targeted_gifts")
public class TargetedGift { 
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "targeted_gift_id_generator")
        @SequenceGenerator(name = "targeted_gift_id_generator", 
         allocationSize 
        = 1)
        @Column(name="targeted_gift_id")
        private int targetedGiftID;

And the University Entity class would like this:

@Data
@Entity
@Table(name ="universities")
public class DatabaseUniversity extends BasicEntity {

    @Column(name = "university_id")
    @Id
    private int universityId;

    @Column(name = "university_name")
    private String universityName;
    @ManyToOne
    @JoinColumn(name = "city_id")
    private DatabaseCity city;
    @Column(name = "country_id")
    private int country_id;
    @Override
    public Integer getId() {
        return universityId;
    }

}

I already have I separate table where I store a university details. In TargetedGift Entity I want to create a CollectionTable which will have OneToMany Relation between CollectionTable and Universities and this relation will be store in a separate table called targeted_univeristy_ids.

I could successfully create the relation table, set the table name as I want, also I could set the join column name (TargeredGift Entity's primary key). But I can't set the name for mapped Entity's (University's) primary key. Hibernate automatically producing a name which is universityid_university_id. I want this to be changed to only university_id'.

I searched in internet for this question and found few threads related to my question which're linked to Stackoverflow which help me to set the name for joinColumn of TargetedGift entity's. Till now columns in my targeted_univeristy_ids look like this:

table name: targeted_univeristy_ids columns name: -targeted_gift_id - universityid_university_id

Here is one thread which has good explanation to rename the corresponding columns.

@ElementCollection
  @CollectionTable(name = "staff_privileges", joinColumns = 
  @JoinColumn(name = "id_user"))
  @MapKeyColumn(name = "privileges_id")
  @Column(name = "privileges_status")
  private Map<Integer, Boolean> privileges; // privilegeId <-> isEnabled

Following the above example, I understood that @joinColumns indicates the current Entity in which it locates, in my case, TargetedGift and the the bottom @olumn indicate the foreign table's primary key, which in my case is 'University's id (university_id). I followed this example and so far got the result as I mentioned above. Currently my code look this:

    @OneToMany(fetch = FetchType.LAZY)
    @CollectionTable(name = "targeted_univeristy_ids" , joinColumns = 
    @JoinColumn(name = "targeted_gift_id", referencedColumnName = 
    "targeted_gift_id"))
   //@MapKeyColumn(name = "university_id")
    @Column(name = "university_id")
    private List<DatabaseUniversity> universityID;

Now I am looking for a solution to set the university's id column name from universityid_university_id to simply university_id?

Upvotes: 0

Views: 1544

Answers (1)

Mamun
Mamun

Reputation: 375

I found the solution. I was wrong creating it as @CollectionTable where I have to create it as @JoinTable. Below is the code, which solved my problem:

    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name="targeted_universities",
            joinColumns = @JoinColumn( name="targeted_gift_id"),
            inverseJoinColumns = @JoinColumn( name="university_id")
    )
    private List<DatabaseUniversity> universities;

Upvotes: 2

Related Questions