Java – hibernate inheritance using different primary keys

I'm trying to use table_ PER_ Class policy creates inheritance, but I want to create a different primary key for each table. Is it possible?

I have a class register, which has millions of instances, some of which are "special" and have different rules for their columns and additional columns

@MappedSuperclass

public abstract class Register {


    @Id
    @Column(nullable = false,unique = true,updatable = false)
    private Long userId;


    private Date checked;

    @Column(nullable = false)
    private RegisterState tipo;
}


@Entity
@AttributeOverrides({ @AttributeOverride(name = "userId",column = @Column(nullable = false,unique = false,updatable = false)) })
public class PotencialRegister extends Register implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;


    @Column(length = 64,nullable = false,unique = false)
    private String referer;
}

For the basic register, I do not need the ID attribute, because I have a unique column, but for the special entity, the column is not unique, so I added an additional attribute

The problem is that hibernate creates a composite primary key using the parent ID (the generated pattern is):

create table PotencialRegister (
        id integer not null,userId bigint not null,checked datetime(6),tipo integer not null,referer varchar(64) not null,primary key (id,userId)
    )  

    create table Register (
        userId bigint not null,primary key (userId)
    )

The column is correct. Schama is what I want, but I want to remove the "Id" member from the potencialregister primary key

Solution

You can create another class without @ ID column and use this class as the base class for each type of register

So your register class looks like:

@MappedSuperclass

public abstract class Register {

    @Column(nullable = false,updatable = false)
    private Long userId;

    private Date checked;

   @Column(nullable = false)
    private RegisterState tipo;
}

Now for your general registration, you can do the following:

@Entity   
 public class NormalRegister extends Register implements Serializable{

    @Id
    public Long getUserId(){
      return super.userId;
    }

     public void setUserId(Long uId){
        super.userId=uId;
      }

   }

Next, define the potencialregister class as:

@Entity
@AttributeOverrides({ @AttributeOverride(name = "userId",updatable = false)) })
public class PotencialRegister extends Register implements Serializable {

    private Integer id;


    @Column(length = 64,unique = false)
    private String referer;

    @Id
    public Long getUserId(){
      return super.userId;
    }

     public void setUserId(Long uId){
        super.userId=uId;
      }

}

With this, you don't have an ID in the base class. All subclasses can define their own ID attribute

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>