0
votes

I'm not able to solve this new exception when I deploy my war into Tomcat 8 (using mvn tomcat:run-war command).

Here is the pasted exception:

GRAVE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mg-em-default' defined in class path resource [WEB-INF/data-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: mistergift_pu] Unable to build Hibernate SessionFactory at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: javax.persistence.PersistenceException: [PersistenceUnit: mistergift_pu] Unable to build Hibernate SessionFactory at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1225) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:119) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:853) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562) ... 21 more

Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.debatz.mistergift.model.User.token, referenced property unknown: com.debatz.mistergift.model.Token.user at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:160) at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ... 29 more

And the two entities (without the getters/setters):

@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>();
}

// ...

And...

@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() { }

// ...

Can you please give me some advice to solve this issue?

Thanks.

3

3 Answers

3
votes

You can't use mappedBy on both sides of the association.

It is incorrect

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

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

Firstly, you should decide which persistent will be owner of the association ( which table will have a foreign key). For an example, if User will have a foreign key column fk_token with Token identifier

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

}

If Token will have a foreign key column fk_user with User identifier

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

}

Updated

The exception is rised at the line OneToOneSecondPass.doSecondPass(Map) because of otherSideProperty == null. Hibernate try to find Token.user property among persistent classes are known to him and fail. So Hibernate doesn't consider Token as a persistent. Reasons can be

  1. Token doesn't have the @Entity annotation.

  2. Token is not mapped in the hibernate.cfg.xml.

  3. Incorrect scanning of packages with persistents, as @magik reported.

Hibernate, obviously has a very unclear and, may be, incorrect error message with this issue.

1
votes

So I mark this topic as resolve. I finally found how to proceed. v.ladynev was right in his answer but I have another mistake which hide the real issue. It was my component-scan annotation which were redirected to a unexistant package (I had renamed my entities package). Thank guys!

0
votes

Try changing your code as given below, its not able to find "com.debatz.mistergift.model.Token.user " , as it works case sensitive.

@OneToOne(mappedBy = "User")

and

@OneToOne(mappedBy = "Token")

I would suggest using hiberanate plugin to generate these dao classes and of couuse you can customise them .But you avoid doing some typo and save time as well.