This discussion is archived
1 Reply Latest reply: May 22, 2013 9:43 AM by cdelahun RSS

Different behavior with TLG with inheritance based entity

633060 Newbie
Currently Being Moderated
Hi,
I've 2 single table inheritance based entities as shown below (both the entities are coherence intercepted):

@Entity
@Table(name = "EMPLOYEE")
@DiscriminatorValue("EMP")
@DiscriminatorColumn(name = "SUBTYPE", discriminatorType = DiscriminatorType.STRING, length = 4)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@CacheInterceptor(value = CoherenceInterceptor.class)
public class Employee {
@Id
private Long id;
private String name;
private String subtype;
}

@Entity
@DiscriminatorValue("MGR")
@CacheInterceptor(value = CoherenceInterceptor.class)
public class Manager extends Employee {
}

When I explicitly set the discriminator column (subtype in this case), the subtype I set was ignored and the assigned discriminator value is persisted in the DB. But in the Grid, a different subtype than the one persisted in the DB is pushed.

i.e.
EntityManager em = emf.createEntityManager();
Employee employee = new Employee();
employee.setId(1L);
employee.setName("some name");
+// Different subtype than the discriminator value for Employee+
employee.setSubtype("MGR");
em.persist(employee);
em.getTransaction().begin();
em.getTransaction().commit();
em.close();

This triggers the following sql statement:
[EL Fine]: 2013-05-22 06:31:28.156--ClientSession(94206285)--Connection(1671491267)--Thread(Thread[main,5,main])--INSERT INTO EMPLOYEE (ID, NAME, SUBTYPE) VALUES (?, ?, ?)
     bind => [1, some name, EMP]

But when I query,
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("select o from Employee o where o.name = 'some name'");
Employee employee = query.getResultList().get(0);
System.out.println(employee.getSubtype());
em.close();

This prints the value of MGR.

Does anyone know why is different subtype pushed to the DB and TLG?
  • 1. Re: Different behavior with TLG with inheritance based entity
    cdelahun Pro
    Currently Being Moderated
    As it currently stands, there are two writable mappings for the "SUBTYPE" database field, which is not allowed in EclipseLink. If it is not throwing a warning, a bug should be entered so that it does.

    Changing this value is not supported, as you cannot change a java object into a different type by changing a flag. You haven't mentioned your setup, but it seems likely getting the cached version of the Employee - one that you just set the subtype attribute to be 'MGR' on back, it should show the version you set. You would need to refresh the object to be sure it is reading in the value from the datastore.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points