jpa中id的一对一断列映射?

时间:2016-08-19 07:33:08

标签: hibernate jpa

我有三个名为用户,个人资料,分支和另外一个用于链接这三个表的表,userprofilebranch表。我的问题是

  1. 分支表是一个主表,我从中获取一些细节并将分支分配给用户,其中分支ID是PK。
  2. 与分支相同我有一个配置文件主表我在配置文件ID为PK时执行相同的操作。
  3. 我在保存用户时有一个用户表我希望在userprofilebranch表中保留branchid,profileid和userid。
  4. User.java

     public class User implements Serializable, UserDetails {
        private static final long serialVersionUID = 1L;
    
        @Id
        @Column(name="USER_ID", nullable=false)
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long userId ;
    
        @Column(name="USERNAME")
        private String userName ;
    
        @Column(name="FIRST_NAME")
        private String firstName ;
    
        @Column(name="LAST_NAME")
        private String lastName ;
    
    
    
        @OneToOne(optional=true, mappedBy="mappedUser")
        private UserProfileBranch mappedUserProfileBranch;
        public User() {
            super();
        }
    
        public void setUserId( Long userId ) {
            this.userId = userId ;
        }
        public Long getUserId() {
            return this.userId;
        }
    
        public void setFirstName( String firstName ) {
            this.firstName = firstName;
        }
        public String getFirstName() {
            return this.firstName;
        }
    
        public void setLastName( String lastName ) {
            this.lastName = lastName;
        }
        public String getLastName() {
            return this.lastName;
        }
    }
    

    UserProfileBranch.java

     public class UserProfileBranch {
    
     @EmbeddedId @AttributeOverrides({
            @AttributeOverride(name="userId", column=@Column(name="USER_ID")),
            @AttributeOverride(name="branchCode", column=@Column(name="BRANCH_CODE")),
            @AttributeOverride(name="profileId", column=@Column(name="PROFILE_ID"))
        })
        private UserProfileBranchComposite userProfileBranchCompositeKey;
    
    
    
        @OneToOne(optional=true,fetch= FetchType.LAZY) @PrimaryKeyJoinColumns({
            @PrimaryKeyJoinColumn(name="USER_ID", referencedColumnName="USER_ID"), 
            @PrimaryKeyJoinColumn(name="BRANCH_CODE", referencedColumnName="BRANCH_CODE"),
            @PrimaryKeyJoinColumn(name="PROFILE_ID", referencedColumnName="PROFILE_ID")
        })
        private User mappedUser;
    
    
       public UserProfileBranchComposite getUserProfileBranchCompositeKey() {
        return userProfileBranchCompositeKey;
      }
    
       public void setUserProfileBranchCompositeKey(
            UserProfileBranchComposite userProfileBranchCompositeKey) {
        this.userProfileBranchCompositeKey = userProfileBranchCompositeKey;
    }
    }
    

    UserProfileBranchCompositeKey

    @Embeddable
    public class UserProfileBranchComposite implements Serializable{
    
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    @Column(name="USER_ID",nullable= false)
    private Long userId;
    
    
    
    @Column(name="BRANCH_CODE",nullable= false)
    private String branchCode;
    
    @Column(name="PROFILE_ID")
    private String profileId;
    
    public UserProfileBranchComposite() {
    
    }
    
    public UserProfileBranchComposite(Long userId) {
        this.userId = userId;
    }
    
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    
    public String getBranchCode() {
        return branchCode;
    }
    
    public void setBranchCode(String branchCode) {
        this.branchCode = branchCode;
    }
    
    public String getProfileId() {
        return profileId;
    }
    
    public void setProfileId(String profileId) {
        this.profileId = profileId;
    }
    }
    

    当我执行代码时出现此错误

      

    引起:org.hibernate.MappingException:已损坏的列映射:mappedUser.id:in.vit.leasing.security.domain.model.UserProfileBranch       在org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:178)       在org.hibernate.persister.entity.AbstractPropertyMapping.initIdentifierPropertyPaths(AbstractPropertyMapping.java:242)       在org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:222)       在org.hibernate.persister.entity.AbstractEntityPersister.initOrdinaryPropertyPaths(AbstractEntityPersister.java:2457)       在org.hibernate.persister.entity.AbstractEntityPersister.initPropertyPaths(AbstractEntityPersister.java:2494)       在org.hibernate.persister.entity.AbstractEntityPersister.postConstruct(AbstractEntityPersister.java:3949)       在org.hibernate.persister.entity.SingleTableEntityPersister。(SingleTableEntityPersister.java:453)       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:422)       在org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)       在org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)       在org.hibernate.internal.SessionFactoryImpl。(SessionFactoryImpl.java:401)       在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:857)

    我duno在哪里我错了。有人可以帮我解决这个问题

1 个答案:

答案 0 :(得分:3)

我的个人经验建议:不要在Hibernate中使用复合键,使用生成的主键。优点:

  1. 更简单的映射,减少头痛
  2. 减少存储空间(一列用于存储外键,而不是三列)
  3. 更好的表现
  4. 如果你想要它很难:尝试将所有者更改为User.java:

    @OneToOne(optional=true)
    @JoinColumns({
        @JoinColumn(name="KEY_USER_ID", referencedColumnName="USER_ID", updateable=false, insertable=false),
        @JoinColumn(name="KEY_BRANCH_CODE", referencedColumnName="BRANCH_CODE", updateable=false, insertable=false),
        @JoinColumn(name="KEY_PROFILE_ID", referencedColumnName="PROFILE_ID", updateable=false, insertable=false)
    })
    private UserProfileBranch mappedUserProfileBranch;