Problem with mapped of the tables at one to one relationship

Status
Nicht offen für weitere Antworten.
G

Guest

Gast
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
Code:
@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
Code:
+----+------------+-----------+-------------------+------------------+
| 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
 
G

Guest

Gast
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

Top Contributor
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?
 
G

Guest

Gast
I think there problem between MySQL and EJB.

Here:

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

Top Contributor
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:

Code:
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);
 
G

Guest

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

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...
Code:
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

Top Contributor
Do you have the hibernate sequence in your database?
Code:
CREATE SEQUENCE hibernate_sequence
  START WITH 1
  INCREMENT BY 1
 
G

Guest

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

Guest

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

Thank you for your helping.
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
N Problem beim initialisieren des Caches Data Tier 0
S JPA Problem mit Cascading Data Tier 1
M Eclipse 4 RCP Hibernate Problem Data Tier 3
C JPA FetchType.LAZY, Relation @OneToMany und Problem mit dem update Data Tier 1
K Problem mit EJBs und Transaktionen Data Tier 0
G JPA: Entity Klasse @JoinColumns Problem Data Tier 2
M JPA Problem: java.sql.SQLSyntaxErrorException: Data Tier 7
H Hibernate Problem mit Lazy Loading bei @OneToMany Collections Data Tier 5
M MySql und JPA-Timestamp Problem Data Tier 8
J Hibernate Problem bei Master-Detail-Tabellen Data Tier 5
A JPA - ManyToMany Problem - keine Unique Mehrfachzuweisungen Data Tier 4
M Problem beim Laden von Objekten, die von anderen Applikationen in eine DB eingefügt wurden Data Tier 5
M Problem mit @Temporal Mapping und SQL Server Data Tier 3
P JPA - HashMap mit Many-to-Many Relation Problem Data Tier 4
B Problem mit @ManyToMany und CascadeType.ALL Data Tier 3
Blackskyliner [JPA][Anfänger] Problem mit Wertzuweisung aus Verbundtabelle Data Tier 2
B Problem mit org.hibernate.LazyInitializationException Data Tier 11
B DatenquellenUpdater extends Thread - Problem mit PermGenSpace Data Tier 5
S Problem beim Insert mit Hibernate Data Tier 9
Y [openJPA] Problem mit Transaktion? Data Tier 2
A @SecondaryTable Problem Data Tier 9
N Problem beim session.flush(); Data Tier 17
Y Postgres und JPA - Primärschlüssel Problem Data Tier 3
P SQL PRoblem Hibernate? Data Tier 8
Y EJB Problem mit Transaktionen Data Tier 7
M Transaction / Session Problem Data Tier 4
G JPA 2.0 Query Problem Data Tier 3
P CORBA Problem bei EJB 3.0 Anwendung in Glassfish v3 Data Tier 7
F Problem mit Hibernate Schema Update Data Tier 2
S Lazy loading Problem Data Tier 2
M Insert-Problem mit JPA/Hibernate Data Tier 4
megachucky JPA - Problem mit Persistence Unit / Context Data Tier 1
H Hibernate Problem Data Tier 4
D Performance Problem mit Prepared Statement Data Tier 6
T Problem mit openJPA Data Tier 7
P Problem mit Data Tier 9
GilbertGrape Cascade Problem (Hibernate) Data Tier 3
C JPA Problem mit attributeOverride und mehrspaltigem PK Data Tier 2
B select "neu" statement Problem (jpql) Data Tier 8
boxi Hibernate Lazy Loading Problem Data Tier 2
M Problem mit Hibernate und SLF4J - NoSuchMethodException Data Tier 3
G Connection Problem - WAS 6.1, Hibernate, OS Authentication Data Tier 1
K Hibernate update-Problem Data Tier 36
J hibernate problem Data Tier 14
N Hibernate - Problem mit Update/Insert Data Tier 4
B Problem mit @PersistenceContext Data Tier 4
algebraiker QuerySyntaxException is not mapped (RCP) Data Tier 2
M Hibernate Dynamische Tables Data Tier 6
S JPA Toplink Generate Tables from Entities erzeugt keine Outputfile Data Tier 9

Ähnliche Java Themen


Oben