EclipseLink的动态映射

待整理

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");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值