@SecondaryTable注释问题

时间:2009-10-29 15:13:11

标签: hibernate orm

我有以下模型,


@Entity
@Table(name = "user")
@PrimaryKeyJoinColumn(name="user_id")
@SecondaryTables({
@SecondaryTable(name = "user_personal_details", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "user_id")}),
@SecondaryTable(name = "user_address", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "user_id")}),
@SecondaryTable(name = "user_contact_info", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "user_id")}),
@SecondaryTable(name = "user_auth_info", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "user_id")})
})
public abstract class User extends Member implements IUser {
    @Column(table="user_personal_details")
    private UserPersonalDetails personalInfo;
    @Column(table="user_address")
    private Address address;
    @Column(table="user_contact_info")
    private UserContactDetails contactDetails;
    @Column(table="user_auth_info")
    private UserAuthInfo authInfo;
...
}

当我尝试插入User的子类时,我得到一个错误,如下所示

10641 [main] ERROR org.hibernate.AssertionFailure - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure:Table user_personal_details not found

请详细说明这种行为。

课程定义: -

ManagedObject类



@Entity
@Table(name="managed_object")
@Inheritance(strategy=InheritanceType.JOINED)

public abstract class ManagedObject implements IManagedObject
{
    @Id
        @Column(name="mo_id", nullable=false, updatable=false)
        private String id;
    @Column(name="mo_name", nullable=false, updatable=true)
        private String name;
        @Column(name="mo_type", nullable=false, updatable=true)
    private String type;
        @Column(name="mo_additional_info", nullable=true, updatable=true)
    private String additionalInfo;
...
}

会员类



@Entity
@Table(name="t_member")
@PrimaryKeyJoinColumn(name="member_id")
public abstract class Member extends ManagedObject implements IMember {

}

其中一个数据/信息类,如UserPersonalDetails



@Embeddable
@Table
public class UserPersonalDetails extends InfoObject{
    private String firstName;
    private String middleName;
    private String lastName;
...
}

更新

在UserPersonalDetails成员中添加了@Column(table="")注释,是的,@Embedded类中有User注释。 我遇到了以下异常:


4469 [main] ERROR org.hibernate.AssertionFailure - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: Table user_personal_details not found

2 个答案:

答案 0 :(得分:1)

删除字段中的所有@Column,并为方法提供@Column。同时仅为辅助表名称字段指定@table(Column="SecondaryTableName")。这个实现解决了我的问题。

答案 1 :(得分:0)

您没有正确映射其他类(例如UserPersonalDetails)。

它们需要映射为@Embedded或真@ManyToOne(或可能是@OneToOne)关联。

现在你正试图将整个班级UserPersonalDetails)映射到单列personalInfo,因为你没有在连接表(user_personal_details)中明确指定名称,这将无效。

更新(根据发布到问题的映射)

您尚未更新User课程,我将假设您已将@Column声明替换为@Embedded。基本上,你需要的是:

// header annotations are the same as above
public abstract class User extends Member implements IUser {
  @Embedded
  private UserPersonalDetails personalInfo;
  // other components go here
  ...
}

@Embeddable
public class UserPersonalDetails extends InfoObject {
  @Column(table="user_personal_details")
  private String firstName;

  @Column(table="user_personal_details")
  private String middleName;

  @Column(table="user_personal_details")
  private String lastName;
  ...
}

如您所见,您需要声明组件的列将映射到的表,因为默认情况下它们都会转到“主”表(例如,您的实体映射到的表)。可以使用嵌入式@Table注释 NOT 完成 - 您需要通过上面显示的@Column@AttributeOverrides来完成,但后者更加混乱。< / p>