@Test @Ignore public void operateAdmMenuThenSuccessfully() throws Exception { List<FieldNode> fields = Arrays.asList(new FieldNode(DynamicEntityType.CI, 1, "idAdmMenu", Integer.class, "id_adm_menu", true), new FieldNode(DynamicEntityType.CI, 1, "classPath", String.class, "class_path"), new FieldNode(DynamicEntityType.CI, 1, "isActive", Integer.class, "is_active"), new FieldNode(DynamicEntityType.CI, 1, "name", String.class, "name"), new FieldNode(DynamicEntityType.CI, 1, "otherName", String.class, "other_name"), new FieldNode(DynamicEntityType.CI, 1, "parentIdAdmMenu", Integer.class, "parent_id_adm_menu"), new FieldNode(DynamicEntityType.CI, 1, "remark", String.class, "remark"), new FieldNode(DynamicEntityType.CI, 1, "seqNo", Integer.class, "seq_no"), new FieldNode(DynamicEntityType.CI, 1, "url", String.class, "url")); byte[] domainBuf = DynamicEntityGenerator.generate("test.domain.AdmMenu", "adm_menu", fields); Class clzz = entityCL.getClass(domainBuf); HibernateJpaEntityManagerFactory ciManagerFactory = new HibernateJpaEntityManagerFactory(Arrays.asList(clzz), dataSource, entityCL, datasourceProperties.getSchema()); EntityManager ciEntityManager = ciManagerFactory.getEntityManagerFactory() .createEntityManager(); Object entity = clzz.newInstance(); BeanMap beanMap = BeanMap.create(entity); beanMap.put("name", "test menu"); beanMap.put("remark", "test remark"); beanMap.put("seqNo", 99); beanMap.put("idAdmMenu", 100); EntityTransaction transaction = ciEntityManager.getTransaction(); transaction.begin(); Object updatedEntity = ciEntityManager.merge(entity); System.out.println("id:" + BeanMap.create(updatedEntity) .get("idAdmMenu")); transaction.commit(); }
public static byte[] generate(String quanlifiedClass, String tableName, Collection<FieldNode> fields) { String classDesc = quanlifiedClass.replace('.', '/'); String className = classDesc.substring(classDesc.lastIndexOf('/') + 1); ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; writeClass(classDesc, tableName, className, classWriter); { fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC, "serialVersionUID", "J", null, new Long(1L)); fieldVisitor.visitEnd(); } fields.forEach(x -> { writeField(classWriter, x); }); writeConstructor(classWriter); fields.forEach(x -> { writeGetter(classWriter, x, classDesc); writeSetter(classWriter, x, classDesc); }); classWriter.visitEnd(); return classWriter.toByteArray(); }
DynamicEntityGenerator implements Opcodes { public static byte[] generate(String quanlifiedClass, String tableName, Collection<FieldNode> fields) { String classDesc = quanlifiedClass.replace('.', '/'); String className = classDesc.substring(classDesc.lastIndexOf('/') + 1); ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; writeClass(classDesc, tableName, className, classWriter); { fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC, "serialVersionUID", "J", null, new Long(1L)); fieldVisitor.visitEnd(); } fields.forEach(x -> { writeField(classWriter, x); }); writeConstructor(classWriter); fields.forEach(x -> { writeGetter(classWriter, x, classDesc); writeSetter(classWriter, x, classDesc); }); classWriter.visitEnd(); return classWriter.toByteArray(); } }
DynamicEntityGenerator implements Opcodes { public static byte[] generate(String quanlifiedClass, String tableName, Collection<FieldNode> fields) { String classDesc = quanlifiedClass.replace('.', '/'); String className = classDesc.substring(classDesc.lastIndexOf('/') + 1); ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; writeClass(classDesc, tableName, className, classWriter); { fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC, "serialVersionUID", "J", null, new Long(1L)); fieldVisitor.visitEnd(); } fields.forEach(x -> { writeField(classWriter, x); }); writeConstructor(classWriter); fields.forEach(x -> { writeGetter(classWriter, x, classDesc); writeSetter(classWriter, x, classDesc); }); classWriter.visitEnd(); return classWriter.toByteArray(); }  }
DynamicEntityGenerator implements Opcodes { public static byte[] generate(String quanlifiedClass, String tableName, Collection<FieldNode> fields) { String classDesc = quanlifiedClass.replace('.', '/'); String className = classDesc.substring(classDesc.lastIndexOf('/') + 1); ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; writeClass(classDesc, tableName, className, classWriter); { fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC, "serialVersionUID", "J", null, new Long(1L)); fieldVisitor.visitEnd(); } fields.forEach(x -> { writeField(classWriter, x); }); writeConstructor(classWriter); fields.forEach(x -> { writeGetter(classWriter, x, classDesc); writeSetter(classWriter, x, classDesc); }); classWriter.visitEnd(); return classWriter.toByteArray(); }  static byte[] generate(String quanlifiedClass, String tableName, Collection<FieldNode> fields); }
DynamicEntityGenerator implements Opcodes { public static byte[] generate(String quanlifiedClass, String tableName, Collection<FieldNode> fields) { String classDesc = quanlifiedClass.replace('.', '/'); String className = classDesc.substring(classDesc.lastIndexOf('/') + 1); ClassWriter classWriter = new ClassWriter(0); FieldVisitor fieldVisitor; writeClass(classDesc, tableName, className, classWriter); { fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC, "serialVersionUID", "J", null, new Long(1L)); fieldVisitor.visitEnd(); } fields.forEach(x -> { writeField(classWriter, x); }); writeConstructor(classWriter); fields.forEach(x -> { writeGetter(classWriter, x, classDesc); writeSetter(classWriter, x, classDesc); }); classWriter.visitEnd(); return classWriter.toByteArray(); }  static byte[] generate(String quanlifiedClass, String tableName, Collection<FieldNode> fields);  }