0

I am working with eclipseLink 2.6 and I have this entity:

@Entity
@Table(name="ESPACIOACADEMICO")
@NamedQuery(name="EntityEspacioacademico.findAll", query="SELECT e FROM EntityEspacioacademico e")
public class EntityEspacioacademico implements Serializable {

    @Id
    @SequenceGenerator(name="ESPACIOACADEMICO_KIDESPACIOACADEMICO_GENERATOR", sequenceName = "sec_espacioacademico", allocationSize = 1 )
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ESPACIOACADEMICO_KIDESPACIOACADEMICO_GENERATOR")
    @Column(name="K_IDESPACIOACADEMICO")
    private long kIdespacioacademico;

    //bi-directional one-to-one association to EntityFranjadocente
    @OneToOne
    @JoinColumns({
        @JoinColumn(name="K_IDDOCENTE", referencedColumnName="K_IDDOCENTE"),
        @JoinColumn(name="K_IDFRANJA", referencedColumnName="K_IDFRANJA", updatable = false, insertable = false)
        })
    private EntityFranjadocente franjadocente;

    //bi-directional one-to-one association to EntityFranjasalon
    @OneToOne
    @JoinColumns({
        @JoinColumn(name="K_IDFRANJA", referencedColumnName="K_IDFRANJA"),
        @JoinColumn(name="K_IDSALON", referencedColumnName="K_IDSALON")
        })
    private EntityFranjasalon franjasalon;

As you can see the column K_IDFRANJA is shared on the two relationships.The database model allow null values on columns K_IDDOCENTE, K_IDFRANJA and K_IDSALON on table ESPACIOACADEMICO, but logically primary keys on franjaDocente and franjasalon can not be null.

With this mapping I get this error when the K_IDSALON is null:

[EL Info]: server: 2015-04-27 20:09:48.872--ServerSession(26114104)--Detected server platform: org.eclipse.persistence.platform.server.NoServerPlatform. [EL Info]: server: 2015-04-27 20:09:51.123--ServerSession(26114104)--Detected server platform: org.eclipse.persistence.platform.server.NoServerPlatform. [EL Info]: 2015-04-27 20:09:52.972--ServerSession(26114104)--EclipseLink, version: Eclipse Persistence Services - 2.6.0.v20150309-bf26070 [EL Info]: connection: 2015-04-27 20:09:54.153--ServerSession(26114104)--/file:/D:/Programas/Eclipse/workspace/tsis/bin/_Tsis login successful [EL Warning]: 2015-04-27 20:09:55.729--ServerSession(26114104)--java.lang.NullPointerException [EL Warning]: 2015-04-27 20:09:55.73--UnitOfWork(9881895)--java.lang.NullPointerException javax.persistence.PersistenceException: java.lang.NullPointerException at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:484) at co.ud.tsis.logica.persistencia.jpa.entities.controladores.EspacioacademicoJpaController.findEspacioacademicoEntities(EspacioacademicoJpaController.java:269) at co.ud.tsis.logica.persistencia.jpa.entities.controladores.EspacioacademicoJpaController.findEspacioacademicoEntities(EspacioacademicoJpaController.java:235) at co.ud.tsis.logica.persistencia.jpa.entities.controladores.EspacioacademicoJpaController.main(EspacioacademicoJpaController.java:443) Caused by: java.lang.NullPointerException at org.eclipse.persistence.queries.ReadObjectQuery.executeObjectLevelReadQuery(ReadObjectQuery.java:452) at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1173) at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:904) at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1132) at org.eclipse.persistence.queries.ReadObjectQuery.execute(ReadObjectQuery.java:425) at org.eclipse.persistence.internal.sessions.AbstractSession.internalExecuteQuery(AbstractSession.java:3267) at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857) at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839) at org.eclipse.persistence.internal.indirection.NoIndirectionPolicy.valueFromQuery(NoIndirectionPolicy.java:326) at org.eclipse.persistence.mappings.ForeignReferenceMapping.valueFromRowInternal(ForeignReferenceMapping.java:2328) at org.eclipse.persistence.mappings.OneToOneMapping.valueFromRowInternal(OneToOneMapping.java:1848) at org.eclipse.persistence.mappings.ForeignReferenceMapping.valueFromRow(ForeignReferenceMapping.java:2178) at org.eclipse.persistence.mappings.ForeignReferenceMapping.readFromRowIntoObject(ForeignReferenceMapping.java:1505) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoObject(ObjectBuilder.java:462) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:1005) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneNormally(ObjectBuilder.java:899) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:852) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:735) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:689) at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:803) at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:944) at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:555) at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1173) at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:904) at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1132) at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:442) at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1220) at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896) at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857) at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839) at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804) at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258) at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:473) ... 3 more

The code to retrieve enities is that:

 private List<EntityEspacioacademico> findEspacioacademicoEntities(boolean all, int maxResults, int firstResult) {
    EntityManager em = getEntityManager();
    try {
        CriteriaQuery<EntityEspacioacademico> cq = em.getCriteriaBuilder().createQuery(EntityEspacioacademico.class);
        cq.select(cq.from(EntityEspacioacademico.class));
        TypedQuery<EntityEspacioacademico> q = em.createQuery(cq);
        if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    } finally {
        em.close();
    }
}

PS: The model is a timetabling problem. Franja is time slot, Docente is the teacher and Salon is the Room. FranjaDocente represents those time slots when the teacher is available, and in the same way, FranjaSalon represents those time slots when the room are available. EspacioAcademico represents an assignment, for example: "Jhon Doe at 6:00 a.m. -8:00 a.m. on the room A with linear Progamming". The records on EspacioAcademico are created by an algorithm, and due to the data, maybe have a partially assignment (time slot null, teacher null or room null). On oracle database, in table EspacioAcademico, i have a composite foreign key to franjaDocente's primary key and franjaSalon's primary key, an it allow null values on franja, docente or salon column (in EspacioAcademico Table). In FranjaDocente or FranjaSalon this is not allowed. The aim of this foreign key is prevent that you can create an assignment on time slot when the teacher, or room, is not available.

My Question is ¿how i can map this relationships if any join column can be null? ¿Should i change the join column approach?¿Can i map this with the @Transformation Annotation?

Cristian García
  • 125
  • 1
  • 1
  • 8
  • Are you removing your questions and reposting them? You should be able to edit existing questions, and it will give you a better reputation if you leave them up and put an answer to them. As for this question, what exactly is the problem? You have a mapping, what problem are you getting with it? Allowing nulls just means your foreign keys can be null - that the franjadocente and/or franjasalon references can be null. – Chris Apr 27 '15 at 12:42
  • Hi, i'm sorry for the others questions, i have no idea how to explain the problem: I need map this relationships regardless the values of the columns ( K_IDDOCENTE, K_IDFRANJA and K_IDSALON) but i get an error when K_IDSALON is null. – Cristian García Apr 28 '15 at 01:21
  • An NPE should not be occuring within in EclipseLink code and would be considered a bug. What is causing it though is uncertain - how can K_IDSALON really be null in your model but not K_IDFRANJA? Does the referenced EntityFranjasalon allow null values for its K_IDSALON field? If K_IDFRANJA has a value, it implies that there exists a EntityFranjasalon instance for it, so maybe this model isn't what you are really going for. How do want to set the K_IDFRANJA field? – Chris Apr 28 '15 at 03:17
  • Hi, the NPE is thrown with eclipseLink 2.5 and 2.6, previously i had eclipseLink 2.2 and the error the error was something like: "The parameter name [K_IDFRANJA in the query's selection criteria does not match any parameter name defined in the query". I add some explain about the model. – Cristian García Apr 30 '15 at 00:51
  • I guess the part that doesn't make sense to me is why K_IDFRANJA would be shared in EntityFranjadocente and EntityFranjasalon as part of their primary keys. You would not get this issue if you used a sequence value as you are for the EntityEspacioacademico entity, and you still haven't quite mentioned what you expect when there is a K_IDFRANJA value but K_IDSALON is null - this appears like there should be a corresponding EntityFranjasalon instance. EclipseLink should not have an error, and I have no clue why it is happening so I suggest filing a bug. – Chris Apr 30 '15 at 13:30
  • Workarounds, other then changing your IDs as suggested, would be to try using fetch joins on the franjasalon and franjadocente relationships when you read in EntityEspacioacademico, or to make these relationships lazy. This might cause a different code path to be used that may or may not have this problem – Chris Apr 30 '15 at 13:32
  • Hi, hhe K_iDFRANJA is shared in EnityFranjaDocente and EntityFranjasalon because the teacher and the room will be avalaible at same time slot. So that K_IDFRANJA depends on EnityFranjaDocente(teacher availability) and/or EntityFranjasalon (room availability), when K_IDFRANJA is not null, is because there is a K_IDDOCENTE and/or K_IDSALON. However, it could have only K_IDOCENTE value but not K_IDFRANJA, or only K_IDSALON but not K_IDFRANJA. Likewise, if there are K_IDDOCENTE and K_IDSALON there must be a K_IDFRANJA. For these reasons you have K_IDFRANJA value but K_IDSALON is null. – Cristian García May 01 '15 at 16:50
  • How can i handle this? Could you explain me fetch joins? – Cristian García May 01 '15 at 16:51

0 Answers0