简体   繁体   中英

Create schema from QueryDSL generated beans and queries

Although I'm not a Ruby user, I really liked the ActiveRecord way of defining a schema in code, and being able to both generate it and query it. I know that this can't be exactly replicated in Java, but I want to do something reasonable.

I've used something like the following from the QueryDSL documentation to generate beans and metadata from my SQL database, using a maven plugin: http://www.querydsl.com/static/querydsl/2.9.0/reference/html/ch02s08.html#d0e1244

<plugin>
  <groupId>com.mysema.querydsl</groupId>
  <artifactId>querydsl-maven-plugin</artifactId>
  <version>${querydsl.version}</version>
  <configuration>
    <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
    <jdbcUrl>jdbc:mysql://localhost:3306/test</jdbcUrl>         
    <packageName>com.example.schema</packageName>
    <targetFolder>${project.basedir}/src/main/scala</targetFolder>
    <exportBeans>true</exportBeans>             
  </configuration>
  <dependencies>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.16</version>
    </dependency>
      <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-scala</artifactId>
        <version>${querydsl.version}</version>
      </dependency>
  </dependencies>
</plugin>  

Now that I've got all these beans and tables, with their keys and foreign key constraints defined, is there a way to generate MySQL commands that will re-create the schema of that table?

If that's not possible, what's a recommended way to distribute the sources and schema in a way that correspond to each other? Should I just have a empty MySQL dump and have tools to generate the beans accordingly? Or should I include the generated source along with an empty schema to import into MySQL? The latter option seems like it could be problematic if versions aren't synced, so I'd appreciate if anyone has any other ideas.

One way could be to package the generated sources and versioned DDL/DML files of your DB schema. We have used DbMaintain for that in some projects successfully.

Deriving the schema data from generated Querydsl sources is not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM