Hibernate一对一映射异常

时间:2017-01-27 22:42:32

标签: java hibernate one-to-one

我正在进行一对一的映射并获取AnnotationException:unknown mappedByin

 @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

我检查了几个堆栈并验证了所有内容。其他帐户和用户类也使用相同的映射。

1 个答案:

答案 0 :(得分:0)

我在我的hibernate配置之后添加了这个Authority实体,忘了在会话工厂配置下映射这个类。当我将其添加为带注释的类时,异常就消失了。

在:

<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>