未知的mappedBy in:com.debatz.mistergift.model.User.token,引用的属性unknown:com.debatz.mistergift.model.Token.user

时间:2016-02-07 23:19:51

标签: java spring hibernate maven tomcat

当我将战争部署到Tomcat 8(使用mvn tomcat:run-war命令)时,我无法解决这个新异常。

以下是粘贴的例外:

  

GRAVE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常   org.springframework.beans.factory.BeanCreationException:创建名称为' mg-em-default'的bean时出错。在类路径资源[WEB-INF / data-context.xml]中定义:init方法的调用失败;嵌套异常是javax.persistence.PersistenceException:[PersistenceUnit:mistergift_pu]无法构建Hibernate SessionFactory       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)       在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)       在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)       at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)       在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)       在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)       at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)       在org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)       在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)       在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)       at java.util.concurrent.FutureTask.run(FutureTask.java:266)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       在java.lang.Thread.run(Thread.java:745)   引起:javax.persistence.PersistenceException:[PersistenceUnit:mistergift_pu]无法构建Hibernate SessionFactory       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1225)       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access $ 600(EntityManagerFactoryBuilderImpl.java:119)       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:853)       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:843)       at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397)       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)       在org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)       在org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)       在org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)       ......还有21个

     

引起:org.hibernate.AnnotationException:未知的mappedBy in:com.debatz.mistergift.model.User.token,引用的属性unknown:com.debatz.mistergift.model.Token.user       在org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:160)       在org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)       在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)       在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)       在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:850)       ... 29更多

两个实体(没有getter / setter):

@Entity
@Table(schema = "mistergift", name = "users")
public class User {

/**
 * The user id.
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

/**
 * The user first name.
 */
@Column(name = "first_name", length = 255, nullable = false)
private String firstName;

/**
 * The user last name.
 */
@Column(name = "last_name", length = 255, nullable = false)
private String lastName;

/**
 * The user email.
 */
@Column(name = "email", length = 100, nullable = false)
private String email;

/**
 * The user password.
 */
@Column(name = "password", length = 255, nullable = false)
private String password;

/** The user role. */
@Column(name = "role", nullable = false)
@Enumerated(EnumType.ORDINAL)
private Role role;

/** The user token. */
@OneToOne(mappedBy = "user")
private Token token;

/**
 *
 */
public User() {
    this.groups = new ArrayList<Group>();
}

// ...

和...

@Entity
@Table(schema = "mistergift", name = "user_token")
public class Token {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "value", length = 75, nullable = false)
private String value;

@Column(name = "expiration_date", nullable = false)
@Temporal(TemporalType.DATE)
private Date expirationDate;

@OneToOne(mappedBy = "token")
private User user;

/**
 *
 */
public Token() { }

// ...

你能给我一些解决这个问题的建议吗?

感谢。

3 个答案:

答案 0 :(得分:2)

您无法在关联的两侧使用mappedBy

不正确

/** The user token. */
@OneToOne(mappedBy = "user")
private Token token; 

@OneToOne(mappedBy = "token")
private User user;

首先,您应该确定哪个持久性将是关联的所有者(哪个表将具有外键)。 例如,如果User的外键列fk_token带有Token标识符

@Entity
@Table(schema = "mistergift", name = "users")
public class User {

    /** The user token. */
    @OneToOne
    @JoinColumn(name = "fk_token")
    private Token token;

}

@Entity
@Table(schema = "mistergift", name = "user_token")
public class Token {

    @OneToOne(mappedBy = "token", fetch = FetchType.LAZY)
    private User user;

}

如果Token的外键列fk_user带有User标识符

@Entity
@Table(schema = "mistergift", name = "users")
public class User {

    /** The user token. */
    @OneToOne(mappedBy = "user")
    private Token token;

}

@Entity
@Table(schema = "mistergift", name = "user_token")
public class Token {

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_user")
    private User user;

}

<强>更新

由于otherSideProperty == null,异常在OneToOneSecondPass.doSecondPass(Map)行上升。 Hibernate尝试在持久化类中查找Token.user属性,并且已知并失败。所以Hibernate并不认为Token是持久性的。原因可以是

  1. Token没有@Entity注释。

  2. Token未映射到hibernate.cfg.xml

  3. 使用持久性文件扫描不正确的软件包,如@magik reported

  4. Hibernate,显然有一个非常不清楚,可能是不正确的错误消息与此问题。

答案 1 :(得分:1)

所以我将此主题标记为已解决。我终于找到了如何继续。 v.ladynev的答案是正确的,但我有另一个错误隐藏真正的问题。这是我的组件扫描注释,它被重定向到一个不存在的包(我已经重命名了我的实体包)。谢谢你们!

答案 2 :(得分:0)

尝试更改下面给出的代码,它无法找到&#34; com.debatz.mistergift.model.Token.user&#34; ,因为它区分大小写。

@OneToOne(mappedBy = "User")

@OneToOne(mappedBy = "Token")

我建议使用hiberanate插件来生成这些dao类,并且可以自定义它们。但是你可以避免做一些拼写错误并节省时间。