8

I'm trying to get sequences with Dynamic Entities to work in EclipseLink and I need some help.

I'm defining my dynamic entity like the following:

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
    EntityManager em = emf.createEntityManager();

    Session session = JpaHelper.getEntityManager(em).getServerSession();
    DynamicClassLoader dcl = DynamicClassLoader.lookup(session);

    Class<?> testClass = dcl.createDynamicClass("org.persistence.Test");

    JPADynamicTypeBuilder test = new JPADynamicTypeBuilder(testClass, null, "TEST");

    test.addDirectMapping("id", long.class, "T_ID");
    test.setPrimaryKeyFields("T_ID");
    test.addDirectMapping("col1", long.class, "T_COL1");
    test.addDirectMapping("col2", int.class, "T_COL2");
    test.addDirectMapping("col3", String.class, "T_COL3");
    test.addDirectMapping("col4", String.class, "T_COL4");
    test.addDirectMapping("col5", double.class, "T_COL5");
    test.addDirectMapping("col6", double.class, "T_COL6");

    DynamicHelper helper = new JPADynamicHelper(em);
    helper.addTypes(true, true, test.getType());

I noticed that everything is created according to the specified. I tried to look for some documentation how to use the database sequences and I noticed the JPADynamicTypeBuilder.configureSequencing(Sequence, String, String) method. But I couldn't find any example on how to do it. I toyed around with this method and I end always with the default sequencing strategy, i.e. a table named SEQUENCE.

I tried with a pre-compiled entity using the @GeneratedValue and @SequenceGenerator and everything works fine, so it's something I'm doing wrong with the dynamic entities.

Does anyone know what I might have been doing wrong?

It seems irrelevant, but I'm telling anyway that my database is Oracle.

Thanks in advance,

Rui

rpvilao
  • 1,116
  • 2
  • 14
  • 31

2 Answers2

1

I do not have the details on your Sequence object so my example is just generic but something like the following should work:

test.configureSequencing(
      new NativeSequence("ORACLE_SEQ_OBJ", 1, 1),
      "ORACLE_SEQ_OBJ",
      "T_ID");
Gordon Yorke
  • 1,996
  • 11
  • 7
0

You can use configureSequencing method on the JPADynamicTypeBuilder to set the sequence.

Here's a good example: http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/branches/2.1/trunk/examples/jpa.employee/eclipselink.example.jpa.employee.dynamic/src/example/EmployeeDynamicMappings.java

I'm still trying to make it work with table Sequencing but it gives me an error:

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A non-read-only mapping must be defined for the sequence number field.
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106