How to map a composite key with JPA and Hibernate?

前端 未结 8 1250
旧时难觅i
旧时难觅i 2020-11-22 10:37

In this code, how to generate a Java class for the composite key (how to composite key in hibernate):

create table Time (
     levelStation int(15) not null,         


        
8条回答
  •  耶瑟儿~
    2020-11-22 10:46

    The primary key class must define equals and hashCode methods

    1. When implementing equals you should use instanceof to allow comparing with subclasses. If Hibernate lazy loads a one to one or many to one relation, you will have a proxy for the class instead of the plain class. A proxy is a subclass. Comparing the class names would fail.
      More technically: You should follow the Liskows Substitution Principle and ignore symmetricity.
    2. The next pitfall is using something like name.equals(that.name) instead of name.equals(that.getName()). The first will fail, if that is a proxy.

    http://www.laliluna.de/jpa-hibernate-guide/ch06s06.html

提交回复
热议问题