@Test public void testPersist() { final Widget widget = new Widget(); widget.setId(123); widget.setName("persistTest"); em.getTransaction().begin(); em.persist(widget); em.getTransaction().commit(); final Widget check = em.find(Widget.class, 123); assertNotNull(check); assertEquals(123, check.getId()); assertEquals("persistTest", check.getName()); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> void persist(final MinijaxEntityManager em, final T entity) { final MinijaxMetamodel metamodel = em.getMetamodel(); final MinijaxEntityType<T> entityType = metamodel.entity((Class<T>) entity.getClass()); final List<MinijaxAttribute<? super T, ?>> attrs = entityType.getAttributesList(); final List<Object> params = new ArrayList<>(); final StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO "); sql.append(entityType.getTable().getName()); sql.append(" ("); boolean first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(", "); } sql.append(attr.getColumn().getName()); first = false; } sql.append(") VALUES ("); first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(','); } sql.append('?'); params.add(attr.write(em, entity)); first = false; } sql.append(")"); executeUpdate(em.getConnection(), sql.toString(), params); updateAssociations(em, entity, entityType.getId().getValue(entity), attrs); }
AnsiSqlDialect implements SqlDialect { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> void persist(final MinijaxEntityManager em, final T entity) { final MinijaxMetamodel metamodel = em.getMetamodel(); final MinijaxEntityType<T> entityType = metamodel.entity((Class<T>) entity.getClass()); final List<MinijaxAttribute<? super T, ?>> attrs = entityType.getAttributesList(); final List<Object> params = new ArrayList<>(); final StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO "); sql.append(entityType.getTable().getName()); sql.append(" ("); boolean first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(", "); } sql.append(attr.getColumn().getName()); first = false; } sql.append(") VALUES ("); first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(','); } sql.append('?'); params.add(attr.write(em, entity)); first = false; } sql.append(")"); executeUpdate(em.getConnection(), sql.toString(), params); updateAssociations(em, entity, entityType.getId().getValue(entity), attrs); } }
AnsiSqlDialect implements SqlDialect { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> void persist(final MinijaxEntityManager em, final T entity) { final MinijaxMetamodel metamodel = em.getMetamodel(); final MinijaxEntityType<T> entityType = metamodel.entity((Class<T>) entity.getClass()); final List<MinijaxAttribute<? super T, ?>> attrs = entityType.getAttributesList(); final List<Object> params = new ArrayList<>(); final StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO "); sql.append(entityType.getTable().getName()); sql.append(" ("); boolean first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(", "); } sql.append(attr.getColumn().getName()); first = false; } sql.append(") VALUES ("); first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(','); } sql.append('?'); params.add(attr.write(em, entity)); first = false; } sql.append(")"); executeUpdate(em.getConnection(), sql.toString(), params); updateAssociations(em, entity, entityType.getId().getValue(entity), attrs); }  }
AnsiSqlDialect implements SqlDialect { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> void persist(final MinijaxEntityManager em, final T entity) { final MinijaxMetamodel metamodel = em.getMetamodel(); final MinijaxEntityType<T> entityType = metamodel.entity((Class<T>) entity.getClass()); final List<MinijaxAttribute<? super T, ?>> attrs = entityType.getAttributesList(); final List<Object> params = new ArrayList<>(); final StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO "); sql.append(entityType.getTable().getName()); sql.append(" ("); boolean first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(", "); } sql.append(attr.getColumn().getName()); first = false; } sql.append(") VALUES ("); first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(','); } sql.append('?'); params.add(attr.write(em, entity)); first = false; } sql.append(")"); executeUpdate(em.getConnection(), sql.toString(), params); updateAssociations(em, entity, entityType.getId().getValue(entity), attrs); }  @Override void createTables(final Connection conn, final MinijaxEntityType<T> entityType); @Override @SuppressWarnings({ "unchecked", "rawtypes" }) void persist(final MinijaxEntityManager em, final T entity); @Override @SuppressWarnings({ "unchecked", "rawtypes" }) T merge(final MinijaxEntityManager em, final T entity); @Override @SuppressWarnings("unchecked") void remove(final MinijaxEntityManager em, final T entity); @Override T find(final MinijaxEntityManager em, final Class<T> entityClass, final Object primaryKey); @Override List<T> getResultList(final MinijaxEntityManager em, final MinijaxNativeQuery<T> query); @Override T getSingleResult(final MinijaxEntityManager em, final MinijaxNativeQuery<T> query); @Override int executeUpdate(final MinijaxEntityManager em, final MinijaxNativeQuery<T> query); @Override List<T> getResultList(final MinijaxEntityManager em, final MinijaxQuery<T> query); @Override T getSingleResult(final MinijaxEntityManager em, final MinijaxQuery<T> query); @Override int executeUpdate(final MinijaxEntityManager em, final MinijaxQuery<T> query); }
AnsiSqlDialect implements SqlDialect { @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> void persist(final MinijaxEntityManager em, final T entity) { final MinijaxMetamodel metamodel = em.getMetamodel(); final MinijaxEntityType<T> entityType = metamodel.entity((Class<T>) entity.getClass()); final List<MinijaxAttribute<? super T, ?>> attrs = entityType.getAttributesList(); final List<Object> params = new ArrayList<>(); final StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO "); sql.append(entityType.getTable().getName()); sql.append(" ("); boolean first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(", "); } sql.append(attr.getColumn().getName()); first = false; } sql.append(") VALUES ("); first = true; for (final MinijaxAttribute<? super T, ?> attr : attrs) { if (attr instanceof MinijaxPluralAttribute) { continue; } if (!first) { sql.append(','); } sql.append('?'); params.add(attr.write(em, entity)); first = false; } sql.append(")"); executeUpdate(em.getConnection(), sql.toString(), params); updateAssociations(em, entity, entityType.getId().getValue(entity), attrs); }  @Override void createTables(final Connection conn, final MinijaxEntityType<T> entityType); @Override @SuppressWarnings({ "unchecked", "rawtypes" }) void persist(final MinijaxEntityManager em, final T entity); @Override @SuppressWarnings({ "unchecked", "rawtypes" }) T merge(final MinijaxEntityManager em, final T entity); @Override @SuppressWarnings("unchecked") void remove(final MinijaxEntityManager em, final T entity); @Override T find(final MinijaxEntityManager em, final Class<T> entityClass, final Object primaryKey); @Override List<T> getResultList(final MinijaxEntityManager em, final MinijaxNativeQuery<T> query); @Override T getSingleResult(final MinijaxEntityManager em, final MinijaxNativeQuery<T> query); @Override int executeUpdate(final MinijaxEntityManager em, final MinijaxNativeQuery<T> query); @Override List<T> getResultList(final MinijaxEntityManager em, final MinijaxQuery<T> query); @Override T getSingleResult(final MinijaxEntityManager em, final MinijaxQuery<T> query); @Override int executeUpdate(final MinijaxEntityManager em, final MinijaxQuery<T> query);  }