telosystools

Open full view…

Generating Code From Existing Model

vortical42
Fri, 20 Dec 2019 16:20:59 GMT

I've been looking over the tutorials and so far they all assume that the code needs to be generated directly from the database. I'm looking to use the telosys tools on a project where I already have JPA model classes and want to covert them into something else ( DTOs in this case. Is it even possible to do the code generation without going through the database? If so, where can I find the documentation for that?

Telosys Team
Mon, 23 Dec 2019 10:18:21 GMT

With Telosys, you can generate the code from 2 types of "model": "DSL model" or "database model". Currently, you cannot generate your code directly from Java classes. You must convert your Java classes to Telosys model (DSL model or database) There are two ways to do this: 1) Create a "DSL model" from scratch and create a new entity for each Java class (this is a manual conversion, acceptable if you don't have many entities) 2) Use your JPA entities (Java classes) to automatically create the database schema on a database (using the JPA property "javax.persistence.schema-generation.database.action" = "drop-and- create "), then use Telosys to create a database model from this database schema. You can do it easily with a light database like "H2" or "Derby"

vortical42
Mon, 23 Dec 2019 14:06:46 GMT

The second option sounds like what I'm after. Is there any documentation or tutorials showing how to do that?

Telosys Team
Tue, 24 Dec 2019 15:31:58 GMT

1) Just define the "persistence.xml" with the correct JPA property : <persistence-unit *name="mydb"* transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <!-- database.action : "none", "create", "drop-and-create", "drop". --> <property name= *"javax.persistence.schema-generation.database.action"* value=*"drop-and-create"* /> </properties> </persistence-unit> 2) Create a Java class with a "main" method just to init the EntityManagerFactory and then create the schema public class CreateDbSchemaFromJPAEntities { public static void main(String[] args) { System.out.println("--- Persistence.createEntityManagerFactory(xx)..."); *EntityManagerFactory emf = Persistence.createEntityManagerFactory("mydb");* System.out.println("--- closing EntityManagerFactory..."); emf.close(); } } 3) Run this class and the schema will be created 4) Use Telosys to create a "DB Model" as usual with the new database

vortical42
Mon, 30 Dec 2019 19:05:02 GMT

Is there a way to do that within the context of an application server? If I inject the entity manager with @PersistenceContext, what do I need to do to get the schema?

vortical42
Mon, 30 Dec 2019 20:02:21 GMT

Alternatively, what do I need to do to run this as a RESOURCE_LOCAL as indicated? I keep getting the error "Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named mydb at javax.persistence.Persistence.createEntityManagerFactory(Persistence.j ava:54) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.j ava:32) at com.tura.product.util.CreateDbSchemaFromJPAEntities.main(CreateDbSchem aFromJPAEntities.java:12)" My persistence.xml looks like this: --- <?xml version="1.0" encoding="utf-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="mydb" transaction-type="RESOURCE_LOCAL"> <class>com.tura.product.domain.Frame</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name= "javax.persistence.schema-generation.database .action" value="drop-and-create" /> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" /> </properties> </persistence-unit> </persistence> ---

Telosys Team
Tue, 31 Dec 2019 09:07:15 GMT

You need a "provider" in the *"persistence.xml"* file Example : <persistence-unit name="h2db" transaction-type="RESOURCE_LOCAL"> *<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>* <!-- Database Properties --> <properties> </properties> </persistence-unit> ============================================ And Hibernate dependency in the *"pom.xml"* Example : <dependencies> <!-- Hibernate --> *<dependency>* *<groupId>org.hibernate</groupId>* *<artifactId>hibernate-entitymanager</artifactId>* *<version>4.3.11.Final</version>* *</dependency>* <!-- Logging : SLF4J is used by Hibernate --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <!-- SLF4J Simple implementation : sends log messages to the console --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.5</version> </dependency> <!-- Database H2 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.190</version> </dependency> </dependencies>

vortical42
Tue, 31 Dec 2019 13:40:11 GMT

I figured it out. I needed to move the persistence.xml file to a different location for the local run ( src/META-INF/ in my case). I can run the application now and it is generating a bunch of 'segments.gen' files in folders that match my package structure. How do I go from that to setting up a datasource and generating the DTOs?

Telosys Team
Fri, 03 Jan 2020 14:13:37 GMT

I don't know this type of file (it looks like Lucene or ElasticSearch files). Which database do you use? I recommend using a light relational database like "Derby" or "H2". Once the schema created you can easily connect and use Telosys to generate the DB-model