如何在hibernate中使用复合PK为JOINED继承设置注释?

时间:2012-03-28 07:55:57

标签: java hibernate jpa hibernate-mapping hibernate-annotations

我刚接触休眠,并且很难用Joined inheritance来设置composite Primary Key。根据我目前的设置,我得到了:

JDBCException:无法插入:LandHolidayPackage

我基本上是在寻找两件事:

  • 是否有继承注释?
  • 复合PK设置是否正确?

数据库设计: HolidayPackageVariants db design image

Reference

以下是我的课程和所涉及的注释:

@Entity
@Table(name = "HOLIDAYPACKAGE")
public final class HolidayPackage {
    private Integer idPackage;
    private String name;

    private Set<HolidayPackageVariant> holidayPackageVariants = new HashSet<HolidayPackageVariant>(0);

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "IDHOLIDAYPACKAGE", nullable = false)
    public Integer getIdPackage() {
        return idPackage;
    }

    @OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.ALL}, mappedBy = "holidayPackage")
    public Set<HolidayPackageVariant> getHolidayPackageVariants() {
        return holidayPackageVariants;
    }

    // ommitted other part of the code
    }

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name="HOLIDAYPACKAGEVARIANT")
public abstract class HolidayPackageVariant {
    private Integer idHolidayPackageVariant;
    private HolidayPackage holidayPackage;
    private String typeHolidayPackage;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="IDHOLIDAYPACKAGEVARIANT", nullable=false)
    public Integer getIdHolidayPackageVariant() {
        return idHolidayPackageVariant;
    }

    @ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
    @JoinColumn(name="IDHOLIDAYPACKAGE", nullable=false)
    public HolidayPackage getHolidayPackage() {
        return holidayPackage;
    }

    @Column(name="TYPEHOLIDAYPACKAGE", nullable=true)
    public String getTypeHolidayPackage() {
        return typeHolidayPackage;
    }

    // ommitted setters, equals hashCode
    }

@Entity
@Table(name="LANDHOLIDAYPACKAGEVARIANT")
public final class LandHolidayPackageVariant extends HolidayPackageVariant{
    private static final String LAND = "LAND";

    protected LandHolidayPackageVariant() {}

    public LandHolidayPackageVariant(HolidayPackage holidayPackage) {
        super(holidayPackage, LAND);
    }
    }

@Entity
@Table(name="FLIGHTHOLIDAYPACKAGEVARIANT")
public final class FlightHolidayPackageVariant extends HolidayPackageVariant{
    private static final String FLIGHT = "FLIGHT";
    private Destination originCity;

    protected FlightHolidayPackageVariant(){}

    public FlightHolidayPackageVariant(HolidayPackage holidayPackage,
            Destination originCity) {
        super(holidayPackage, FLIGHT);
        setOriginCity(originCity);
    }

    @ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
    @JoinColumn(name="IDDESTINATION", nullable=false)
    public Destination getOriginCity() {
        return originCity;
    }

    // ommited other setters etc functions
    }

1 个答案:

答案 0 :(得分:0)

您注释了属性而不是字段。 JPA默认尝试访问这些字段。如果您希望JPA使用您必须使用@AccessType(AccessType.Field)注释该类的字段。