I have a war file with my webapp and I need to generate the database tables in MySQL in the moment when I have copied the war to the webapps folder from Tomcat (assuming Tomcat is running).
Is there a way to do this, can you help me with some information about this?
PS. It's a very simple webapp, using just JDBC, i don't use any ORM frameworks. Is it possible with JDBC?
If you just want to update the schema, but don't want to introduce something as heavyweight as Hibernate, there are some good alternatives:
Flyway - Provide migration scripts with your application to update it at deployment time.
Liquibase - Provide XML description of your schema, and allow Liquibase to update the schema.
Flyway requires you to write the SQL to migrate between different versions of your application. It doesn't generate any DDL (SQL for modifying the schema) at all. It just runs the SQL you give it. If you're comfortable with SQL, and want to be able to use all the features your database provides, I can strongly recommend this.
The code to start the migration can be put into a ServletContextListener. Example code that could be used there is given on the Flyway Application Integration page.
Liquibase works by reading an XML file describing the schema changes necessary to upgrade your database to the latest schema. These changes are applied in order. This means that your schema and changes are more likely to be portable to other databases, but it's arguably harder to take advantage of features that are specific to your database.
Liquibase provides its own ServletContextListener that you can use. See the ServletListener page in the docs.
If you do want to introduce an ORM, you could look at:
Hibernate - ORM with lots of features (and quite a few dependencies)
EclipseLink - previously Oracle TopLink. This seems more polished than Hibernate, but has fewer features.
Do the job in the contextInitialized() method of a custom ServletContextListener class. This will be invoked when the webapp starts up.
#WebListener
public class Config implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent event) {
// Just write/call JDBC code here the usual way to create database/tables if not exists.
}
// ...
}
That said, using an ORM framework like JPA/Hibernate really saves you from a lot of JDBC boilerplate code and headaches.
sounds like a case for : http://www.hibernate.org/
Related
Is it possible to turn off DDL generation on the class level vs the full application?
I have a reporting app that we have thus far had the setting in application.properties
jpa.generateDdl=false
We effectively have several different views in this app that populate java objects annotated with #Entity. This works great.
But now we want to add other objects which we DO want to persist.
If I switch on jpa.generateDdl=true, then it will generate tables for these views, which we want to avoid. Is there a way to prevent this from happening, while still allowing other objects to persist?
No that's not possible.
As written in the Hibernate documentation you shouldn't relay on the feature for production applications.
Although the automatic schema generation is very useful for testing
and prototyping purposes, in a production environment, it’s much more
flexible to manage the schema using incremental migration scripts.
https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#schema-generation
I would recommend to use Liquibase or Flyway to use for database migrations.
I'm new with Spring and I would like to use Spring Data with Jpa, Maven and Mysql in Eclipse environment. Then I'm going to use Tomcat as application server
I read a lot of tutorial but all of them are different from each other therefore don't understand what I have really to do. At the moment I'm confusing between these technology and DAO/DAL structure
Must I use Hibernate?
I have to use it with java JPA and then I have to add Spring data?
If anyone has an example or a tutorial I thank him.
Official spring data link
For spring-data-jpa,maven,mysql,hibernate,eclipse example tutorial
Hibernate is optional, it depends on your use case what you want to do. Its a ORM for mapping your db tables into java objects and have lot of other features like session caching, second level caching, criteria queries, validation, search/filters etc.
If you are just testing queries to load data you can use spring jpa queries directly, you will find examples in spring-data-jpa link
I have written a REST webservice using JAX-RS and I currently prepare my test database using DbUnit. However, if I would now deploy may application, this would not fit my needs anymore. Thus, I am looking for a maven plugin that lets me handle the preparation and update of the production database. So I need something that creates my tables and inserts default data when I deploy my service for the first time and updates the tables when I deploy new releases, when the service is running.
There are a couple of useful frameworks for this usecase.
Have a look at:
FlyWay
and Liquibase
Both can handle your use case quite neatly. The main difference is the way how migrations are defined, flyway uses SQL and Javacode based migrations, Liquibase uses XML.
My personal preference lies with FlyWay, as I find it more natural.
I just wanted to hear the opinion of Hibernate experts about DB schema generation best practices for Hibernate/JPA based projects. Especially:
What strategy to use when the project has just started? Is it recommended to let Hibernate automatically generate the schema in this phase or is it better to create the database tables manually from earliest phases of the project?
Pretending that throughout the project the schema was being generated using Hibernate, is it better to disable automatic schema generation and manually create the database schema just before the system is released into production?
And after the system has been released into production, what is the best practice for maintaining the entity classes and the DB schema (e.g. adding/renaming/updating columns, renaming tables, etc.)?
It's always recommended to generate the schema manually, preferably by a tool supporting database schema revisions, such as the great Liquibase. Generating the schema from the entities is great in theory, but were fragile in practice and causes lots of problems in the long run(trust me on this).
In productions it's always best to have manually generated and review the schema.
You make an update to an entity and create a matching update script(revision) to update your database schema to reflect the entity change. You can create a custom solution(I've written a few) or use something more popular like liquibase(it even supports schema changes rollbacks). If you're using a build tool such as maven or ant - it's recommend to plug the db schema update util into the build process so that fresh builds stay in sync with the schema.
Although disputable, I'd say that the answer to all 3 questions is: let hibernate automatically generate the tables in the schema.
I haven't had any problems with that so far. You might need to clean some field up manually from time to time, but this is no headache compared to separately keeping track of DDL scripts - i.e. managing their revisions and synchronizing them with entity changes (and vice-versa)
For deploying on production - an obvious tip - first make sure everything is generated OK on the test environment and then deploy on production.
Manually, because:
Same database may be used by different applications and not all of
them would be using hibernate or even java. Database schema should
not be dictated by ORM, it should be designed around the data and
business requirements.
The datatypes chosen by hibernate might not be best suited for the application.
As mentioned in an earlier comment, changes to the entities would require manual intervention if data loss is not acceptable.
Things such as additional properties (generic term not java
properties) on join tables work wonderfully in RDBMS but are
somewhat complex and inefficient to use in an ORM. Doing such a
mapping from ORM -> RDBMS might create tables that are not
efficient. In theory, it is possible to build the exact same join
table using hibernate generated code, but it would require some
special care while writing the Entities.
I would use automatic generation for standalone applications or databases that are accessed via the same ORM layer and also if the app needs to be ported to different databases. It would save lot of time in by not requiring one to write and maintain DB vendor specific DDL scripts.
Like Bozhidar said, don´t let Hibernate create&update the database schema.
Let your application create and update the database schema.
For java the best tool to do this is Flyway. You need to create one or more SQL files with DDL statements which are describing your database schema. These SQL files are then executed by Flyway. For more information look at the site of Flyway.
I believe that a lot of what is being discussed or argued here should also be related to if you are more confortable with the code-first or the database-first approach.
Personally, I am more intended to go for latter and, making a reference to Single Responsibility Principle (SRP), I prefer having DB specialist handling the DB and an application specialist handling the application, than having the application handling the DB. Additionally, I am of the opinion that taking too many shortcuts will work fine at the beginning but create unmanageable problems as things grow/evolve.
may i know in eclipse, is there any feature that will auto generate domain objects with all table relationship properly mapped in class?
can provide me with some reference articles on this?
You can use something like Hibernate to accomplish this
This plugin set for Eclipse called Hibernate Tools for Eclipse and ANT will do most of the work for you.
In particular it will do Reverse Engineering: The most powerful feature of Hibernate Tools is a database reverse engineering tool that can generate domain model classes and Hibernate mapping files, annotated EJB3 entity beans, HTML documentation or even an entire JBoss Seam application in seconds!
Telosys code generator does this kind of job.
It's an Eclipse plugin, it uses the database schema to create a light model
that is used to generate the Java code.
There are some predefined templates available on GitHub (for JPA, POJO, Documentation, Spring MVC, etc )
See http://www.telosys.org
and http://marketplace.eclipse.org/content/telosys-tools
Templates : https://github.com/telosys-templates-v3
Articles about code generation with Telosys :
https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
https://dzone.com/articles/telosys-a-code-generation-tool-by-laurent-guerin
You can use Hibernate Tools 3.0.0.GA either via Eclipse or ANT to auto-generate your hibernate domain entities directly from your database tables.
See tutorial here :
http://docs.jboss.org/tools/3.0.0.GA/en/hibernatetools/html_single/index.html
I have a solution for you i.e to create auto generate domain objects with all table relationship properly mapped in class ...Try Dal4j yes you can find it in sourceforge.net/p/dal4j/wiki/ DAL4j is a Command Line and Framework tool that can be used to reverse engineer a MySQL or SQLServer database schema into a set of JPA Entity Beans.
DAL4j can be useful for scenarios where there is an existing database schema but a technology other that JPA is used by applications to interact with the database. DAL4j can provide an easy way to migrate your code base from other technologies such as JDBC or Hibernate to JPA.
The beans generated can be 1 or two types: Simple or Framework. Simple beans are standard pojo classes managed by your application using JPA semantics. Framework generated pojos use the DAL4j framework DAO generic to simplify CRUD operations.
DAL4j provides optional hooks to allow you integrate encryption/decryption of data fields that must be encrypted in the database.
Last, DAL4j provides a set of Generic classes that can be used to simplify creation of Session Beans which perform CRUD operations using generated Entities.
I think you will find this article feasible....
You want an object relational mapping of which Hibernate is the most popular for Java. The hibernate tools are typically better for taking annotated classes and using them to generate a schema, as opposed to vice versa, which is what you sound like you're doing. I suspect you'll be doing a lot of hand-annotating if you're working with a legacy DB schema.
if you use grails, you can generate domain objects with GRAG http://sourceforge.net/projects/grag
I use eclipse for java development, but when it comes to generating domain entities I use Net beans.
Create an EJB module, and then right click and generate entities.
You need to set up the database also you can select the tables you want visually.
Regards
Lyju
It feels like another rather common question that people always run into.
The link below links to a blog detailed enough for me to learn how to generate entities from database schema the first time.
http://shengwangi.blogspot.com/2014/12/how-to-create-java-classes-from-tables.html
Just in case, the following link refers to eclipse help page. This link should never expire:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jpt.doc.user%2Ftasks021.htm
I downloaded JBoss and failed to understand how it works. I think the plugin that I used is Hibernate Tools but I am not sure as I did not install any new plugin for this purpose. I am using Eclipse Luna for EE.
Hope this helps.
I got so tired of manually coding this kind of stuff so I made a tool to generate models, dao, and dao implementation from a schema. It's oriented towards spring boot and only tested on MySQL, but for those that don't want to use Hibernate and just want to work with jdbc/sql and JdbcTemplate, or just want POJOs with getter/setters generated for tables, then this could perhaps be something to kick off the coding.
Called Jassd (Java Automated Spring Source-code for Databases generator), I'm "jazzed" to introduce this tool: https://github.com/aforslund/jassd