0

我有一个用户获得一些个人资料的用例:所以是一对多关联。

这是通过伪 sql 中的这些遗留表映射的。

TABLE USER
(
  ID PK
  SURNAME VARCHAR2(45 BYTE) NOT NULL,
  NAME    VARCHAR2(45 BYTE) NOT NULL,
  USER_ID NUMBER
)

TABLE USER_PROFILES
(
  USER_ID      NUMBER                           NOT NULL,
  ID_PROFILO   NUMBER                           NOT NULL,
  DESCRIPTION  VARCHAR2(32 BYTE)
)

这是java映射:

@Entity
@Table(name = "USER)
public class User{

    @Id
    @Column(name = "ID")    
    private String id;

    @OneToMany(fetch= FetchType.EAGER)
    @JoinColumns({ @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")})
    private List<UtenteProfilo> profiles;
}

和 userprofiles 类

@Entity
@Table(name = "USER_PROFILES")
@IdClass(UserProfile.UserProfileId.class)
public class UserProfile implements java.io.Serializable {
    @Id
    @Column(name = "USER_ID", nullable = false)
    private Long userId;

    @Id
    @Column(name = "ID_PROFILO", nullable = false, insertable = false, updatable = false)
    private Long profiloId;

    private String description;

    static public class UserProfileId implements Serializable {
        @Id
        @Column(name = "USER_ID", nullable = false)
        Long userId;
        @Id
        @Column(name = "ID_PROFILO", nullable = false)
        Long profiloId;
    }
}

当我从 UserDao 获得用户时,我收到此异常:

Exception Description: The @JoinColumns on the annotated element [field profiles] from the entity class [User] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.

问题是在源实体(获得 1-N 关联的实体)上,我只有一列可以进行连接。

我试图更改目标实体的 ID;所以在 user_profiles 类中,只声明 user_id 作为键:但是以这种方式,当 jpa 创建查询时,它只处理一个 user_profiles 出现,使关系为 1-1。

知道如何解决问题并使关联正常工作吗?

亲切的问候马西莫

4

1 回答 1

1

JPA 要求在所有关系中使用 Id。您可以将 userId 设置为 User 中的 Id,或者使用 Id 作为外键而不是 UserId。

您似乎正在使用 EclipseLink。因此,您可以按原样进行映射,但您很可能需要使用 DescriptorCustomizer 和 OneToMany 映射上的 API 来定义外键(EclipseLink 支持使用备用键,而不是通过 JPA 注释)。

从技术上讲,EclipseLink 应该支持备用键的注释,所以请为此记录一个错误并投票。

于 2011-09-06T14:42:39.757 回答