Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @Inheritante with @OneToOne And @ID - Internal error
@Inheritante with @OneToOne And @ID - Internal error [message #1004358] Tue, 22 January 2013 22:44 Go to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
Hi all. I have an Entity that servers as base for another, with Inheritante with JOINED strategy: This entity haves an @OneToOne mapping as @Id:

@Entity
@Table(name="enti_empresa", schema="cadastro")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="tipo_empresa", discriminatorType=DiscriminatorType.CHAR, length=1)
@DiscriminatorValue("M")
public class Empresa implements DefaultEntity<PessoaJuridica> {

	private static final long serialVersionUID = 7175307548694472116L;

	@Id
	@OneToOne(fetch=FetchType.EAGER, optional=false)
	@JoinColumn(name="enti_empresa_pk_fk", updatable=false, nullable=false)
	private PessoaJuridica id;


And, this is an subclass:

@Entity
@Table(name="enti_filial", schema="cadastro")
@PrimaryKeyJoinColumn(name="enti_empr_filial_pk_fk")
@DiscriminatorValue("F")
public class Filial extends Empresa {
	private static final long serialVersionUID = 4753041286089829659L;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="enti_empr_matriz_fk", nullable=false)
	private Empresa matriz;



On create EntityManagerFactory, I got this error:

2013-01-23 01:35:02.878] ERROR http-bio-8080-exec-1         System.err                                                        Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.EntityManagerSetupException 
[2013-01-23 01:35:02.878] ERROR http-bio-8080-exec-1         System.err                                                        Exception Description: Predeployment of PersistenceUnit [AutmixPU] failed. 
[2013-01-23 01:35:02.878] ERROR http-bio-8080-exec-1         System.err                                                        Internal Exception: java.util.NoSuchElementException 
[2013-01-23 01:35:02.878] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1541) 
[2013-01-23 01:35:02.878] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1532) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:98) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:108) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	... 109 more 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.EntityManagerSetupException 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        Exception Description: Predeployment of PersistenceUnit [AutmixPU] failed. 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        Internal Exception: java.util.NoSuchElementException 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:221) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	... 113 more 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        Caused by: java.util.NoSuchElementException 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at java.util.ArrayList$Itr.next(ArrayList.java:794) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.getPrimaryKeyField(MetadataDescriptor.java:991) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.MetadataAccessor.getReferencedField(MetadataAccessor.java:432) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.MetadataAccessor.getReferencedField(MetadataAccessor.java:420) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.addMultipleTableKeyFields(EntityAccessor.java:201) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processInheritancePrimaryKeyJoinColumns(EntityAccessor.java:1007) 
[2013-01-23 01:35:02.879] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.inheritance.InheritanceMetadata.processInheritanceSubclass(InheritanceMetadata.java:298) 
[2013-01-23 01:35:02.880] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.inheritance.InheritanceMetadata.process(InheritanceMetadata.java:233) 
[2013-01-23 01:35:02.880] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processTableAndInheritance(EntityAccessor.java:1210) 
[2013-01-23 01:35:02.880] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(EntityAccessor.java:619) 
[2013-01-23 01:35:02.880] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1686) 
[2013-01-23 01:35:02.880] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:532) 
[2013-01-23 01:35:02.880] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:550) 
[2013-01-23 01:35:02.880] ERROR http-bio-8080-exec-1         System.err                                                        	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1469) 



If I change the type of type of the Superclass ID to Integer, or Long for example, this problem does not occours.
Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004489 is a reply to message #1004358] Wed, 23 January 2013 05:47 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
i think, you need to define CustomSequencing, take a look at http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing
on this way you can define your PessoaJuridica id object
Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004512 is a reply to message #1004489] Wed, 23 January 2013 06:27 Go to previous messageGo to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
Hi Filipp! Thanks for answer!

But, the PessoaJuridica already haves and ID. In fact, PessoaJuridica extends Pessoa with a normal Inheritance JOINED relationship too. Pessoa haves an sequence Generator:

@DiscriminatorValue("J")
@PrimaryKeyJoinColumn(name="pess_juridica_pk_fk")
public class PessoaJuridica extends Pessoa {


@Entity
@Table(name="pess_pessoa", schema="cadastro")
@SequenceGenerator(name="sequ_pessoa", sequenceName="sequ_pessoa", schema="cadastro", allocationSize=1)
@DiscriminatorColumn(name="discriminador", length=1, discriminatorType=DiscriminatorType.CHAR)
@Inheritance(strategy=InheritanceType.JOINED)
public class Pessoa implements DefaultEntity<Long> {

	private static final long serialVersionUID = 5868486120336695252L;

	@Id
	@Column(name="pessoa_pk")
	@GeneratedValue(generator="cadastro.sequ_pessoa", strategy=GenerationType.AUTO)	
	private Long id;


Isn't correct to assume that the ID of "Empresa" or "Filial", the last entities in the chain will get their database level ids from the "pessoa" sequence generator??
Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004517 is a reply to message #1004512] Wed, 23 January 2013 06:41 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
hmm... i'm not sure, but you can't make it so. Empresa and PessoaJuridica two different entities, every entity need one ID.
if you need PessoaJuridica.ID in your Empresa, define it as pessoaJuridicaId and on extra id for Empressa - like
public class Empresa implements DefaultEntity<PessoaJuridica> {

	private static final long serialVersionUID = 7175307548694472116L;

	@Id
	@Column(name="pessoa_pk")
	@GeneratedValue(generator="cadastro.sequ_pessoa", strategy=GenerationType.AUTO)	
	private Long id;

	@OneToOne(fetch=FetchType.EAGER, optional=false)
	@JoinColumn(name="enti_empresa_pk_fk", updatable=false, nullable=false)
	private PessoaJuridica pessoaJuridicaId;

Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004525 is a reply to message #1004517] Wed, 23 January 2013 06:59 Go to previous messageGo to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
But, in that case, the "Empresa" ID, must be the same ID of a "PessoaJuridica", because on database, empresa primary key, its a primary key foreign-key of their PessoaJuridica.

Do you think that its will be a cleaner solution:

public class Epresa {

        @Id
	@Column(name="enti_empresa_pk_fk")
        private Long id;

	@OneToOne(fetch=FetchType.EAGER, optional=false)
	@PrimaryKeyJoinColumn(name="enti_empresa_pk_fk")
	private PessoaJuridica pessoaJuridicaId;

        .....
 
        public void setPessoaJuridica(PessoaJuridica pj) {
             this.pessoaJuridica = pj;
             this.id = pj.getId();
        }

Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004532 is a reply to message #1004525] Wed, 23 January 2013 07:13 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
i found a better solution, look http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships
Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004540 is a reply to message #1004532] Wed, 23 January 2013 07:29 Go to previous messageGo to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
But, according to this doc, I'm mapping the entity correctly:

Quote:

JPA 2.0

The Id used for the object will be derived from the target object's Id. If the Id is a single value, then the source object's Id is the same as the target object's Id.


Because that, I've mapped my @OneToOne as an ID. The @OneToOne Join Column is my Primary Key/Foreign Key for this table. The problem occours on the "Filial" entity, that extends a table that haves an @OneToOne as ID no??
Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004548 is a reply to message #1004540] Wed, 23 January 2013 07:40 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
maybe... try to add @IdClass(PessoaJuridica.class) in your Empresa class
Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004553 is a reply to message #1004548] Wed, 23 January 2013 07:50 Go to previous messageGo to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
This does not work too Sad

I have another entities with @OneToOne with @Ids on this domain. Only this class is throwing errors. This is the only class with a @OneToOne ID that haves @Inheritance. I think that this is the problem. Only removing @Inheritance subject annotations of Empresa and Filial, this error stops.

This topic says that @Id with @OneToOne is perfectly supported by JPA 2.0

http://stackoverflow.com/questions/5805306/onetoone-between-two-tables-with-shared-primary-key

..

[Updated on: Wed, 23 January 2013 07:52]

Report message to a moderator

Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004576 is a reply to message #1004553] Wed, 23 January 2013 08:43 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 995
Registered: July 2009
Senior Member
Using @Id on your relationship mapping is supported assuming that the referenced entity has a basic ID that can be used as the ID class. This looks like a bug in processing the @PrimaryKeyJoinColumn with inheritance when the root is using a reference mapping as the ID - it is expecting a field mapping instead.

Please file a bug and vote to have the issue fixed. You might try adding the referencedColumnName value as a workaround:
@PrimaryKeyJoinColumn(name="enti_empr_filial_pk_fk", referencedColumnName="enti_empresa_pk_fk")


Re: @Inheritante with @OneToOne And @ID - Internal error [message #1004583 is a reply to message #1004576] Wed, 23 January 2013 08:57 Go to previous messageGo to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
Hi Chris.

Readin eclipselink source, I found that it try to find the Primary Key field using the referenced column name. I try to do what you ask

@PrimaryKeyJoinColumn(name="enti_empr_filial_pk_fk", referencedColumnName="enti_empresa_pk_fk")


But this does not work too.

If I create a @Id with a normal @Column pointing to the PK_FK column, this error seems to be fixed, but I cannot map the @OneToOne relation. Only if I duplicate my mappings. This will be the only workaround?

PS: I've reported this bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=398881

[Updated on: Wed, 23 January 2013 09:20]

Report message to a moderator

Re: @Inheritante with @OneToOne And @ID - Internal error [message #1005133 is a reply to message #1004583] Thu, 24 January 2013 09:37 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Why do you have, updatable=false, nullable=false on your id, you should remove this,

@Id
@OneToOne(fetch=FetchType.EAGER, optional=false)
@JoinColumn(name="enti_empresa_pk_fk", updatable=false, nullable=false)
private PessoaJuridica id;


James : Wiki : Book : Blog : Twitter
Re: @Inheritante with @OneToOne And @ID - Internal error [message #1005164 is a reply to message #1005133] Thu, 24 January 2013 10:21 Go to previous messageGo to next message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
Hi James. I was trying to simulate id ID behavior. Once inserted, it cannot be changed. This is a wrong design?
Re: @Inheritante with @OneToOne And @ID - Internal error [message #1006042 is a reply to message #1005164] Tue, 29 January 2013 10:46 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

insertable=false will definitely cause an issue, nullable=false is fine, updatable=false may cause an issue, you could try removing it.


James : Wiki : Book : Blog : Twitter
Previous Topic:SQLServerException: Some part of your SQL statement is nested too deeply.
Next Topic:TABLE_PER_TENANT
Goto Forum:
  


Current Time: Sun Apr 20 09:27:32 EDT 2014

Powered by FUDForum. Page generated in 0.02077 seconds