2017-01-27 14 views
2

私は1対1のマッピングとなっAnnotationExceptionやっている:不明mappedByinHibernateは1対1のマッピング例外

@Entity 
@Table(name="user") 
public class User { 
    @NotNull(message="Username cannot be blank") 
    @Size(max=45,min=3,message="Username should be between 3 and 45 characters") 
    @Pattern(regexp="^\\w{8,}$",message="Username can only consist of characters,numbers and underscores") 
    @Id 
    private String username; 
    @NotNull(message="Password cannot be blank") 
    @Size(max=45,min=3,message="Password should be between 3 and 45 characters") 
    @Column 
    private String password; 
    @NotNull(message="Email cannot be blank") 
    @Pattern(regexp= "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" 
       + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",message="Please provide a valid email address") 
    @Column 
    private String email; 
    @NotNull(message="Name cannot be blank") 
    @Size(max=45,min=3,message="Name should be between 3 and 45 characters") 
    @Column 
    private String name; 
    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    private Account account;  
    @OneToOne(mappedBy = "userEntity", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    private Authorities authorities; 
    @Column 
    private boolean enabled; 

@Entity 
@Table(name="authorities") 
public class Authorities { 

    @Column 
    private String authority; 
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    @JoinColumn(name = "username") 
    private User userEntity; 
    @Id 
    private String id; 

と例外が

Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.globalbank.model.User.authorities, referenced property unknown: com.globalbank.model.Authorities.userEntity 
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:153) 
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697) 
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930) 
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372) 
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454) 
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 
... 73 more 
です

私はいくつかのスタックをチェックし、すべてを検証しました。他のAccountクラスとUserクラスでも同じマッピングが機能していました。

答えて

0

休止状態の設定後にこのAuthoritiesエンティティを追加しました。このクラスをセッションファクトリの設定でマップするのを忘れてしまいました。注釈付きクラスとしてこれを追加すると、例外がなくなりました。前

<bean id="hibernate4AnnotatedSessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
     <list> 
      <value>com.globalbank.model.Account</value> 
      <value>com.globalbank.model.User</value> 
      <value>com.globalbank.model.Transaction</value> 
     </list> 
    </property> 

後:

<bean id="hibernate4AnnotatedSessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
     <list> 
      <value>com.globalbank.model.Account</value> 
      <value>com.globalbank.model.User</value> 
      <value>com.globalbank.model.Transaction</value> 
      <value>com.globalbank.model.Authorities</value> 
     </list> 
    </property> 
関連する問題