Rosen Martev
Rosen Martev

Reputation: 519

JPA Problems mapping relationships

I have a problem when I try to persist my model. An exception is thrown when creating the EntityManagerFactory:

Blockquote javax.persistence.PersistenceException: [PersistenceUnit: ASD] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34) at project.serealization.util.PersistentManager.createSession(PersistentManager.java:24) at project.serealization.SerializationTest.testProject(SerializationTest.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at junit.framework.TestCase.runTest(TestCase.java:168) at junit.framework.TestCase.runBare(TestCase.java:134) at junit.framework.TestResult$1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:232) at junit.framework.TestSuite.run(TestSuite.java:227) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.hibernate.HibernateException: Wrong column type in asd.entity_one for column ENTITY_TWO_ID. Found: double, expected: bigint at org.hibernate.mapping.Table.validateColumns(Table.java:284) at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1116) at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139) at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:349) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) ... 24 more

The code for the two entities is as follows:

@Entity
public class EntityOne 
{
@OneToOne(cascade = CascadeType.ALL, targetEntity = EntityTwo.class)
@JoinColumn(name = "ENTITY_TWO_ID")
private EntityTwo entityTwo;

... 
}


@Entity
public class EntityTwo
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ELEMENT_ID")
private Long element_id;

...
}

Can someone please tell me what is wrong with my model? Thanks in advance.

Upvotes: 1

Views: 3250

Answers (1)

Pascal Thivent
Pascal Thivent

Reputation: 570295

Well, the message is self-explaining: the column type doesn't match the type used at the Java level. If you want to sore a Long, use a BIGINT as column type.

Update: I tested your code with Hibernate as JPA provider and ENTITYONE.ENTITY_TWO_ID is generated as a BIGINT column. Maybe you generated your table and changed the Java type later without regenerating the physical model. My suggestion would be to drop the table and to try again.

Upvotes: 2

Related Questions