3

there is the possibility of existing entity class with values generate an insert statment?

EDIT: I mean to generate an insert statement for an instance of an entity class, to execute that statement separately.

Thanks in advance

user1167253
  • 813
  • 1
  • 11
  • 27

1 Answers1

8

With Fastnate you can create SQL statements for entities without a connection to a database:

public String createSQL() {
    // Create your entity
    TestEntity entity = new TestEntity();
    entity.setSomeProperty("Example");
    ...

    // Write your entity as SQL
    StringWriter result = new StringWriter();
    EntitySqlGenerator sqlGenerator = new EntitySqlGenerator(result);
    sqlGenerator.write(entity);
    return result.toString();
}
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
  • Please how do I exclude the generator from adding the primary key column to the generated SQL string. This is because my table has an id with auto generated column. – Uchenna Nwanyanwu Mar 25 '19 at 11:44
  • @Uchenna, could you create a separate question for this (and only add a reference here)? – Tobias Liefke Mar 26 '19 at 15:33
  • Fastnate looks promising, but it doesn't yet seem to handle EmbeddedIds. There's an issue tracking it in the Github project, but it appears to keep getting postponed from release to release. Is support for EmbeddedId fields imminent, or should we consider another solution? – Kevin Swan Sep 10 '19 at 14:55
  • 1
    I think that you should ask that question on the issue page. How should we know that there is the demand for an issue, if no one asks for it? – Tobias Liefke Sep 11 '19 at 15:38