gpt4 book ai didi

java - 来自具有属性访问权限的外键的 JPA 复合主键

转载 作者:行者123 更新时间:2023-12-01 08:53:04 25 4
gpt4 key购买 nike

我是 JPA 的新手,请跟着我。

显然,如何通过属性访问方式从外键创建复合主键是没有问题的。

问题

如果我使用如下例所示的属性访问类型,是否还必须为引用的 FK 定义 getter 和 setter?

我不认为是这样,但是 official documentation Java EE6 就是这样做的。

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide

A composite primary key class has the following characteristics:

  • It is a POJO class.
  • It must be public and must have a public no-argument constructor.
  • If you use property-based access, the properties of the primary key class must be public or protected.
  • It must be serializable.
  • It must define equals and hashCode methods.
  • The semantics of value equality for these methods must be consistent with the database equality for the database types to which the key is mapped.

You can make the composite primary key class either an embedded classowned by the entity class, or a nonembedded class whose fields you mapto multiple fields or properties of the entity class. In the lattercase, the names of primary key fields or properties in the compositeprimary key class and those of the entity class must correspond andtheir types must be the same.

我修改了这个示例,因为我想使用 FK。

Example 7-2 Embeddable Composite Primary Key Class

@Embeddable
public class EmployeePK implements Serializable {
private String name;
private long id;

public EmployeePK() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public int hashCode() {
return (int) name.hashCode() + id;
}

public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof EmployeePK)) return false;
if (obj == null) return false;
EmployeePK pk = (EmployeePK) obj;
return pk.id == id && pk.name.equals(name);
}
}

Example 7-3 JPA Entity With an Embedded Composite Primary Key Class

@Entity
@Access(AccessType.PROPERTY)
public class Employee implements Serializable {
EmployeePK primaryKey;

public Employee() {
}

@EmbeddedId
public EmployeePK getPrimaryKey() {
return primaryKey;
}

public void setPrimaryKey(EmployeePK pk) {
primaryKey = pk;
}

@ManyToOne
@MapsId("id")
private classWithPKid fkobject1;

@ManyToOne
@MapsId("name")
private classWithPKname fkobject2;
...
}

最佳答案

JPA 规范 (2.3.2) - 显式访问类型

When Access(PROPERTY) is applied to an entity class, mapped superclass, or embeddable class, mapping annotations may be placed on the properties of that class, and the persistence provider runtime accesses persistent state via the properties defined by that class. All properties that are not annotated with the Transient annotation are persistent. When Access(PROPERTY) is applied to such a class, it is possible to selectively designate individual attributes within the class for instance variable access. To specify a persistent instance variable for access by the persistence provider runtime, that instance variable must be designated Access(FIELD). The behavior is undefined if mapping annotations are placed on any instance variables defined by the class for which Access(FIELD) is not specified. Persistent state inherited from superclasses is accessed in accordance with the access types of those superclasses.

JPA 规范 (2.3.3) - 可嵌入类的访问类型

The access type of an embeddable class is determined by the access type of the entity class, mapped superclass, or embeddable class in which it is embedded (including as a member of an element collection) independent of whether the access type of the containing class has been explicitly specified or defaulted. A different access type for an embeddable class can be specified for that embeddable class by means of the Access annotation as described above.

当实体类的 AccessType 设置为 PROPERTY 时,提供程序将使用这些方法来获取持久状态和映射数据。当类的 AccessType 为 PROPERTY 时,持久性提供程序没有义务查找字段上的映射注释。另请注意,AccessType 是由 Embedded 类继承的,这就是 EmployeePK 也需要定义 getter/setter 的原因。根据规范中的规定,当实体类使用 Access(PROPERTY) 时,您应该执行以下操作之一:

  1. 为 FK 字段定义 getter/setter 方法并将映射放置在 getter 方法上

    示例:

    @Entity
    @Access(AccessType.PROPERTY)
    public class Employee {
    private EmployeePK primaryKey;

    private ClassWithPKid fkobject1;

    @EmbeddedId
    public EmployeePK getPrimaryKey() {
    return primaryKey;
    }

    @ManyToOne
    @MapsId("id")
    public ClassWithPKid getFkobject1() {
    return fkobject1;
    }
    }
  2. 或者,在持久字段上显式定义 Access(FIELD)

    示例:

    @Entity
    @Access(AccessType.PROPERTY)
    public class Employee {
    private EmployeePK primaryKey;

    @ManyToOne
    @MapsId("id")
    @Access(AccessType.FIELD)
    private ClassWithPKid fkobject1;

    @EmbeddedId
    public EmployeePK getPrimaryKey() {
    return primaryKey;
    }
    }

关于java - 来自具有属性访问权限的外键的 JPA 复合主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42254584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com