开发者

@SecondaryTable annotation problem

开发者 https://www.devze.com 2022-12-09 23:16 出处:网络
I have following model, @Entity @Table(name = \"user\") @PrimaryKeyJoinColumn(name=\"user_id\") @SecondaryTables({

I have following model,


@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;
...
}

When I try to insert a subclass of User I get an error shown below

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

Please shed some light on this behavior.

Class Definitions:-

ManagedObject Class



@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;
...
}

Member Class



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

}

One of the Data/Info Class like UserPersonalDetails



@Embeddable
@Table
public class UserPersonalDetails extends InfoObject{
    private String firstName;
    开发者_运维知识库private String middleName;
    private String lastName;
...
}

Update:

Added @Column(table="") annotation in UserPersonalDetails members and yes I do have @Embedded annotation in User class. I have encountered following exception:


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


remove all @Column in the fields and give @Column for the methods. Also specify the @table(Column="SecondaryTableName") only for the secondary table names fields. This implementation solved my problem.


You did not map other classes (like UserPersonalDetails) correctly.

They either need to be mapped as @Embedded or as true @ManyToOne (or, possibly, @OneToOne) associations.

The way it is right now you're trying to map an entire class (UserPersonalDetails) to a single column (personalInfo because you didn't specify name explicitly) in joined table (user_personal_details), which will not work.

Update (based on mappings posted to question)

You haven't updated your User class, I'm going to assume you've replaced the @Column declarations with @Embedded in it. Basically, what you need is:

// 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;
  ...
}

As you can see, you need to declare the table your component's columns will be mapped to because by default they all go to the "main" table (e.g. the one your entity is mapped to). That can NOT be done using @Table annotation on embeddable - you need to do it via @Column as shown above or via @AttributeOverrides but the latter is messier.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号