# Problem with mapped of the tables at one to one relationship



## Guest (8. Aug 2007)

Two tables INTERPRETER_IMAGE AND INTERPRETER_DATA are related to one table INTERPRETER. If a entity bean Interpreter with new entities Interpreter_image and Interpreter_data in a persist() method call, it should automatically be wired between entities. But my program didn't work.

Entity

```
@Entity
@Table(name="INTERPRETER")
public class Interpreter implements Serializable
{
	private static final long serialVersionUID = 1;
 
	@Id
	@Column(name="ID")
	@GeneratedValue
	private long id;
	
	@Column(name="FIRST_NAME")
	private String firstName;
	
	@Column(name="LAST_NAME")
	private String lastName;
	
	@OneToOne(fetch=FetchType.LAZY,cascade={CascadeType.ALL})
	@JoinColumn(name="INTERPRETER_IMAGE")
	private InterpreterImage interpreterImage;
	
	@OneToOne(fetch=FetchType.LAZY,cascade={CascadeType.ALL})
	@JoinColumn(name="INTERPRETER_DATA")
	private InterpreterData interpreterData;
 
@Entity
@Table(name="INTERPRETER_DATA")
public class InterpreterData implements Serializable
{
 
	private static final long serialVersionUID = 1;
 
	@Id
	@Column(name="ID")
	private int id;
 
	@Column(name="CERTIFICATE")
	private String certificate;
@Entity
@Table(name="INTERPRETER_IMAGE")
public class InterpreterImage implements Serializable
{
	private static final long serialVersionUID = 1;
 
	@Id
	@Column(name="ID")
	private int id;
	
	@Column(name="GALLERY_FILENAME")
	private String galleryFilename;
```

Select Statement

```
+----+------------+-----------+-------------------+------------------+
| ID | FIRST_NAME | LAST_NAME | INTERPRETER_IMAGE | INTERPRETER_DATA |
+----+------------+-----------+-------------------+------------------+
|  1 | Sascha     | Gerdes   |                 0 |                0 |
+----+------------+-----------+-------------------+------------------+
1 row in set (0.00 sec)
 
mysql> SELECT ID FROM INTERPRETER_IMAGE;
+----+
| ID |
+----+
|  1 |
+----+
1 row in set (0.00 sec)
 
mysql> SELECT ID FROM INTERPRETER_DATA;
+----+
| ID |
+----+
|  1 |
+----+
1 row in set (0.00 sec)
```

I hope you find my mistake. 

Manu


----------



## Guest (10. Aug 2007)

Hello

I don't solve that problem. hmmmm?? I am using EJB with MySQL. Mayby that is some different to other database??

Manuel


----------



## kleiner_held (10. Aug 2007)

I don't think that there is a problem with MySQL.
How do you create your new Interpreter with the nested entities InterpreterImage and InterpreterData?


----------



## Guest (10. Aug 2007)

I think there problem between MySQL and EJB. 

Here:


```
CREATE TABLE INTERPRETER_IMAGE (
	ID BIGINT NOT NULL AUTO_INCREMENT,
	GALLERY_FILENAME VARCHAR(30),
	GALLERY_IMAGE BLOB,
	PROFIL_FILENAME VARCHAR(30),
	PROFIL_IMAGE BLOB,
	INDEX(ID)
);

CREATE TABLE INTERPRETER_DATA (
	ID BIGINT NOT NULL AUTO_INCREMENT,
	CERTIFICATE VARCHAR(50),
	INDEX(ID)
);

CREATE TABLE INTERPRETER(
	ID BIGINT NOT NULL AUTO_INCREMENT,
	FIRST_NAME VARCHAR(30),
	LAST_NAME  VARCHAR(30),
	INTERPRETER_IMAGE_ID BIGINT,
	INTERPRETER_DATA_ID BIGINT,
	PRIMARY KEY(ID)
);
```

Manuel


----------



## kleiner_held (10. Aug 2007)

What kind of application server do you use?
I think you should remove the AUTO_INCREMENT attribute of your ID columns in the database and annotate the ID property of all entities with @GeneratedValue. This will delegate the id generation to the EJB container.

Then try to create a set of new entities:


```
InterpreterData interpreterData = new InterpreterData();
interpreterData.setCertificate("ABC");
entityManager.persist(interpreterData);

InterpreterImage interpreterImage = new InterpreterImage ();
interpreterImage .setGalleryFilename("ABC");
entityManager.persist(interpreterImage );

Interpreter interpreter = new Interpreter ();
interpreter.setFirstName("ABC");
interpreter.setLastName("ABC");
interpreter.setInterpreterImage (interpreterImage);
interpreter.setInterpreterData(interpreterData);
entityManager.persist(interpreter);
```


----------



## Guest (10. Aug 2007)

Sorry, it don't work because of ID fields of interpreter_data. I use MySQL 5.1.15-beta and jboss 4.0.2.GA

again Code:


```
package de.kirchedlau.ponte.ejb.entities;

import java.io.Serializable;

@Entity
@Table(name="INTERPRETER")
public class Interpreter implements Serializable
{
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name="ID")
	@GeneratedValue
	private long id;
	
	@Column(name="FIRST_NAME")
	private String firstName;
	
	@Column(name="LAST_NAME")
	private String lastName;
	
	
	@OneToOne(cascade={CascadeType.PERSIST,CascadeType.REMOVE})
	@JoinColumn(name="INTERPRETER_IMAGE_ID")
	private InterpreterImage interpreterImage;
	
	@OneToOne(cascade={CascadeType.PERSIST,CascadeType.REMOVE})
	@JoinColumn(name="INTERPRETER_DATA_ID")
	private InterpreterData interpreterData;

	//setter and getter
}

package de.kirchedlau.ponte.ejb.entities;

import java.io.Serializable;

@Entity
@Table(name="INTERPRETER_DATA")
public class InterpreterData implements Serializable
{

	private static final long serialVersionUID = 1L;

	@Id
	@Column(name="ID")
	@GeneratedValue
	private int id;

	@Column(name="CERTIFICATE")
	private String certificate;
        // getter and setter
}

package de.kirchedlau.ponte.ejb.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;


@Entity
@Table(name="INTERPRETER_IMAGE")
public class InterpreterImage implements Serializable
{
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name="ID")
	@GeneratedValue
	private int id;
	
	@Column(name="GALLERY_FILENAME")
	private String galleryFilename;
	
	@Lob
	@Column(name="GALLERY_IMAGE")
	private byte[] galleryImage;
	
	@Column(name="PROFIL_FILENAME")
	private String profilFilename;
	
	@Lob
	@Column(name="PROFIL_IMAGE")
	private byte[] profilImage;

        //setter and getter
}
```

Error Message...

```
12:02:15,410 WARN  [JDBCExceptionReporter] SQL Error: 1364, SQLState: HY000
12:02:15,410 ERROR [JDBCExceptionReporter] Field 'ID' doesn't have a default value
12:02:15,445 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
javax.faces.FacesException: Error calling action method of component with id _idJsp0:_idJsp24
	at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
	at javax.faces.component.UICommand.broadcast(UICommand.java:109)
	at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
	at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
	at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
	at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
	at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:147)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
	at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
	at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
	at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
	at java.lang.Thread.run(Thread.java:619)
Caused by: javax.faces.el.EvaluationException: Exception while invoking expression #{interpreterDataImageHandler.submitMyProfil}
	at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:156)
	at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:61)
	... 29 more
Caused by: javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [de.kirchedlau.ponte.ejb.entities.InterpreterData]
	at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
	at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
	at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
	at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:102)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:263)
	at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:58)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:102)
	at $Proxy160.createInterpreter(Unknown Source)
	at de.kirchedlau.ponte.InterpreterDataImageHandler.submitMyProfil(InterpreterDataImageHandler.java:63)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:132)
	... 30 more
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert: [de.kirchedlau.ponte.ejb.entities.InterpreterData]
	at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
	at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
	at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:175)
	at de.kirchedlau.ponte.ejb.sessions.ContentBean.createInterpreter(ContentBean.java:304)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
	at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
	at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:46)
	at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
	... 54 more
Caused by: org.hibernate.exception.GenericJDBCException: could not insert: [de.kirchedlau.ponte.ejb.entities.InterpreterData]
	at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2093)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2573)
	at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:47)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
	at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
	at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
	at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
	at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
	at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
	at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
	at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:212)
	... 69 more
Caused by: java.sql.SQLException: Field 'ID' doesn't have a default value
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
	at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440)
	at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:251)
	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
	... 83 more
```


----------



## kleiner_held (10. Aug 2007)

Do you have the hibernate sequence in your database?

```
CREATE SEQUENCE hibernate_sequence
  START WITH 1
  INCREMENT BY 1
```


----------



## Guest (10. Aug 2007)

I don't have the hibernate sequence in my database. I tried to create sequence but it fails. Is the sytnax ok?


----------



## Guest (10. Aug 2007)

Now I solved my problem. I tried to add fetch=FetchType.EAGER into @OneToOne, then it is working.

Thank you for your helping.


----------

