简体   繁体   中英

How to use primary key of multiple tables as primary key of third table in hibernate

I have two tables as following

Post table

post_table

Comment table

评论表

Now i want to create another table called Activity table which will store aggregate values of likes performed on post or comment. So i want to use activity id field which must be EITHER post id or comment id and type field which could be post/comment (string).

活动表

I want to make composite primary key of both this fields. How should i do this in Hibernate?

You can make use of @IdClass to make a composite key with those two fields. First, you need to define a new class that will hold the composite key fields:

public class ActivityKey implements Serializable {
    protected Integer activity_id;
    protected String type;

    public ActivityKey () {}

    public ActivityKey (Integer activity_id, String type) {
        this.activity_id= activity_id;
        this.type= type;
    }
}

And then, your Activity class must look like this:

@Entity
@IdClass(ActivityKey.class)
class Activity {
    @Id
    private Integer activity_id;
    @Id
    private String type;

    //Other fields
}

There are restrictions to apply when defining the primary key class (ActivityKey in this example). This class must be serializable and must define both hashCode() and equals() methods. Also, it must be public and have a public no-arg constructor.

I didn't understand what's your approach with the activity table exactly, but you can create a composite primary key in hibernate, suppose you want to combine employee_id and dept_id to form a primary key. For this, I will use Lombok. We need to create a separate class that has properties that need to be combined and class must implements Serializable and must override equals() and hashCode().

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name = "EMPLOYEE_TBL")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    @EmbeddedId
    private EmployeePKId employeePKId;
    private String name;
    private String email;
    private String phone;
}

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Embeddable;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Embeddable
public class EmployeePKId implements Serializable {
    private int employeeId;
    private int deptId;
}

@Embeddable public class BookId implements Serializable {

private String author;
private String name;

// standard getters and setters

}

@Entity public class Book {

@EmbeddedId
private BookId id;
private String genre;
private Integer price;

//standard getters and setters

}

the above example may help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM