I have created some mysql databases in mysql.
Now I am trying to get them into my web application by using the play framework.
I added the mysql configs in the application.conf, added the dependency for the mysql driver in the build.sbt, created my first model and added the models packages as the ebean default in the application.conf.
Now when I go into my browser I get this error:
I`m a little confused right now, because I do not want to create a new table, but use the one I created already.
Any idea what I am doing wrong??
Play's default behaviour during development is to manage your database via the evolutions plugin. You define your initial schema in conf/evolutions/default/1.sql and then apply subsequent modifications in 2.sql, 3.sql etc etc. Whenever there are changes to these evolution files the plugin will attempt to run these on the database, which is what you're seeing here (although it looks like an error, it's really just trying to be helpful.)
If you want to manage the schema yourself (and you probably should on a production DB, for example) add evolutionplugin=disabled to the application.conf file.
Related
Is there any way to automatically create database functions using jpa/hibernate when i use spring.jpa.hibernate.ddl-auto=create ?
I've a number of custom functions (defined in a separated script file in the project) I use on my custom #Query in my repositories, but those functions needed to be create manually
You might think "what is the problem just run the script after created the database" but this must be done for every environment of the system: dev, test, prod... also it makes impossible to automatically test using in memory database (since when i hit run junit creates the database, hibernate creates the tables in the database, but nobody creates the functions so the repository wont work properly)
So how can I automatically create custom database functions using jpa/hibernate
You can add an import.sql file in your classpath.
Hibernate will create the schema and then run it on the selected db.
You can find more details in the documentation.
Note though that the main purpose for schema generation in Hibernate is prototyping or testing.
Bonjour,
I am working on changing me Java application from using postgres to an embedded database. I would like the application to deploy with an initial set of data in the database. In the past during installation I have executed an sql script to fully generate the schema and insert the data in to my tables.
Ideally (becasue I don't really want to work out how to connect to the embedded database to generate it) I want to let JPA create my schema for the first time, and when it does I then want to be able to run my SQL to insert the data.
My search has turned up the obvious hibernate and JPA properties that allow running of an SQL script.
Firstly I found when using "hibernate.hbm2ddl.auto" you can define an import.sql file this made me very happy for a day until I realised it only works with create and not with update. My application when using postgres had this set to update. And what i would really like is for it to know if it's had to create the schema and if it has then run the import.sql. No Joy though.
I then moved on to using "javax.persistence.schema-generation.database.action" set to "create" I figured using the JPA specification was probably wiser anyway and so I defined "javax.persistence.sql-load-script-source" the spec says for "create"
The provider will create the database artifacts on application
deployment. The artifacts will remain unchanged after application
redeployment.
This lead me to believe it would do exactly what I wanted, only create the tables "on application deployment" however when I ran my tests using this, each test (creating a new spring context) tried to just create all the tables again and obviously failed, which made me realise application deployment didn't mean what i thought it meant (wishful thinking) and now I realise that JPA doesn't seem to even have an equivalent of Hibernates "update" property, so it's always going to generate the tables?
What I want is to have my tables and data generated when you first spin up the app and for subsequent executions to know the data is there and use it, I am assuming it's too much to hope for that this exists, but i'm sure that this must be a common requirement? So my question is what is the general recommended way to achieve the goal of allowing JPA to create my schema but being able to insert some data in to a db that persists between executions?
The answer is flyway. It is a database migration library, and if you are using Spring boot it is seamlessly integrated, with regular Spring you have to create a bean, which get a reference to the connection pool, creates a connection and does the migration.
Flyway creates a table so it keeps track of which scripts has already been applied to the database, and the scripts are simply part of the resources.
We normally use JPA to generate the initial script. This script becomes V1__initial.sql, if we need to add some data we can add V2__addUsers.sql and V3__addCustomers.sql etc.
Later when we need to rename columns or add additional tables, we simply add new scripts as part of the War file, and when the application is loaded Flyway looks at it's internal table, to see the current version, and then applies any new scripts to bring it up to de desired version.
In Spring the code would look like this
private void performFlywayMigration(DataSource dataSource) {
Flyway flyway = new Flyway();
flyway.setLocations("db/migration");
flyway.setDataSource(dataSource);
log.debug("Starting database migration.");
flyway.migrate();
log.debug("Database migration completed.");
MigrationInfo current = flyway.info().current();
if (current.getState() == MigrationState.FUTURE_SUCCESS) {
log.warn("The Database schema is version " + current.getVersion() + ", this application expects version " + flyway.getBaselineVersion().getVersion());
}
}
In general you should not JPA to create tables directly. because you sometimes need to modify the scripts, for instance on Sybase Varchar(255) means 255 bytes, so if you are storing 2 or 3 byte Unicode chars, you need more space - JPA implementation does not account for that (last time I checked).
How to setup a simple embedded database in a spring(data)+maven project?
I need to develop a simple graphical application that read some data files and display pretty stuff about it interactively. The data is very repetitive with a little hierarchical structure. However I still don't know how I will need to access it.
For these reasons, I want to store it in a database so that I can later use DB query to access the data with query filter. (it also seems a good idea to develop a persistent layer)
Because it is for a little application, I want to use an in-memory DB.
I am quite new to java (using proper dev framework) and database. But I worked on a project using spring, spring-data, JPA, etc... I did not really understand how it worked internally and would not be able to setup it up, but I found it very practical.
Now, I found lots of docs and tutorial on internet about that, but I didn't understand enough to know how to adapt them to my need. What (I think) I want is:
to use maven+spring
spring data (I guess) to use Entity, JpaRepository and Autowired stuff
an independent program, thus starting from a Application.main method
as little and simple dependencies as possible
an embedded DB (+fast+light if possible)
genericity is nice
What I fill to be lost with are:
where should I put what properties/xml-declaration
how are all the dependencies working together (spring, spring-data, h2, hsqldb, ...)
I found this project https://github.com/wrpinheiro/spring-jpa-embedded-db that looks to fit, but:
there are way too many dependencies that (I think) I don't need, thus don't want
I don't know how to start a program with it
I don't get the org.springframework.stereotype.Service thing
nor the javax.inject.Inject
I think that if you look at this project you can start building what you need
http://spring.io/guides/gs/accessing-data-rest/#initial
Its maven (or gradle), has enbeded db, spring-jpa and runs as a jar that starts its own tomcat server (you can change it into a war build if you want)
Also you can use this service(?) that spring provides to create the starting build for your project:
http://start.spring.io
You provide them with what you want to build and then the code and required files are generated :D
Pretty neat.
I have a website written in JSF backed by MySQL database running on Tomcat 7. Now there is only one missing part - project first setup/installation. I want my war when deployed for the first time to offer you installation/first time setup with following steps:
Setup database - enter mysql parameters needed to successfully connect to mySQL server.
Write those parameters into some external file for further use (of course encrypted).
Install database - take a file with SQL inside that creates all the tables in database.
Create first user etc.
Delete installation files.
Similar steps are used in PHP Content Management systems like Drupal. I know perfectly how to work with files in Java. I also know, that I can't change content inside a jar once it's deployed and running, so I have to put my files with SQL and database parameters somewhere else.
My questions are
Where can I put these configuration files to make them readable ? And how ?
Is there another way to achieve this goal ? What is commonly used by Java developers ?
Thank you for your answers.
You can use JPA(Java persistent API) and put all this configuration on persistance.xml and set the schema generation to create Table also the things related to role and user is dependent to application server.
JPA use ORM(object relational mapping) to map between you objects (entity) and database tables
I'm using H2 with Hibernate to generate in-memory DB on the fly for unit-testing.
I managed to create the DB successfully, and everything is working ok.
But I have an issue I don't know how to approach.
I need to load reference data to the DB for testing prior to the execution of the tests.
I have this data sored as a SQL insert's file which I need to run only once in real time envirnemnt, however, because the DB is generated every time from scratch I need to figure out how to insert the data on runtime.
The data is quite simple, it's countries lists, states list, etc.
Whats the best way to do it ?
btw, everything is working underneath Spring framework.
For your tests you could execute an init script on creation of the connection.
http://www.h2database.com/html/features.html#execute_sql_on_connection
From the question tags I see you're using Hibernate. You can add a file named "import.sql" to your classpath (i.e. in src/main/resources if you're using a Maven project layout).
From Spring documentation
In addition, a file named import.sql in the root of the classpath will
be executed on startup if Hibernate creates the schema from scratch
(that is if the ddl-auto property is set to create or create-drop).
This can be useful for demos and for testing if you are careful, but
probably not something you want to be on the classpath in production.
It is a Hibernate feature (nothing to do with Spring).