待整理
http://stackoverflow.com/questions/5598728/sequences-with-dynamic-entities-in-eclipselink
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());
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic
Dynamic Configuration using API
Alternatively developers can construct dynamic persistence units using API. A complete example of the same employee demo is available here.
The basic building blocks of constructing dynamic entities using API are:
1. Create the Dynamic Class
The dynamic class is an ASM generated subclass of Dynamic entity and it is created with a DynamicClassLoader as:
DynamicClassLoader dcl = new DynamicClassLoader(currentLoader); Class<?> employeeClass = dcl.createDynamicClass(packagePrefix + "Employee");
2. Create Dynamic Type
Using a JPADynamicTypeBuilder as a factory a new type can be constructed
JPADynamicTypeBuilder address = new JPADynamicTypeBuilder(addressClass, null, "D_ADDRESS");
3. Add the Mappings
Finally the mappings are added:
address.setPrimaryKeyFields("ADDR_ID"); address.addDirectMapping("id", int.class, "ADDR_ID"); address.addDirectMapping("street", String.class, "STREET"); address.addDirectMapping("city", String.class, "CITY"); address.addDirectMapping("province", String.class, "PROV"); address.addDirectMapping("postalCode", String.class, "P_CODE"); address.addDirectMapping("country", String.class, "COUNTRY"); address.configureSequencing("ADDR_SEQ", "ADDR_ID");
Relationships
In addition to these basic steps you will also need to re-use the types when defining relationships between them. To create the Employee to address 1:1 both types are required and used as:
OneToOneMapping addressMapping = employee.addOneToOneMapping("address", address.getType(), "ADDR_ID"); addressMapping.setCascadeAll(true); addressMapping.setIsPrivateOwned(true);
Usage Examples
Bootstrap
In order to bootstrap with dynamic persistence some additional persistence unit properties must be specified.
- Note: Container managed persistence not supported
Map<String, Object> properties = new HashMap<String, Object>(); properties.put(PersistenceUnitProperties.CLASSLOADER, dcl); properties.put(PersistenceUnitProperties.WEAVING, "static"); Persistence.createEntityManagerFactory(persistenceUnit, properties);
DynamicEntity API
DynamicEntity employee = em.createNamedQuery("Employee.findMin", DynamicEntity.class).getSingleResult(); String firstName = employee.<String>get("firstName"); DynamicEntity address = employee.<DynamicEntity>get("address"); Collection<DynamicEntity> phones = employee.<Collection<DynamicEntity>>get("phoneNumbers");