Is present some configuration properties for mybatis in order to create/update database on application startup like does "hibernate.hbm2ddl.auto" ?
Well, The MyBatis Migration Tool might meet your needs, it sounds a little different than Hibernate though.
Related
I want to create my Database (MySQL) using my Java Spring boot #entities & classes defined already with all the required fields and associations. Is it possible to do automatically (By a make migration or migrate command mechanism) ? or do i have to explicitly write the SQL queries e.g the Create Table ones.
PS : tools such as flyway arent of a big help as they require to write SQL code despite defined JAVA Code.
Thank you,
Spring Boot Uses Hibernate under the hood as an ORM mapping tool.
Hibernate indeed has such a feature:
In plain hibernate:
hibernate.hbm2ddl.auto is the parameter that you're looking for: use the create as a value
Other values also can come handy, read this SO thread for more information
Spring boot names this parameter slightly different but the logic behind stays the same
Update 1
*Based on Op's comment:
Unfortunately I don't have any access to code that works with the relational databases. But given Spring boot driven application you probably already have src/main/resources/application.properties (or yaml).
So you should: Read the relevant chapter of spring boot configuration
Then,
In the application.properties (or yml) put
spring.jpa.hibernate.ddl-auto=create
And start the application. It should create the tables during the hibernate startup if everything else is configured correctly.
If
We would like to support two databases MySQL and Cassandra to our application. Our application will be built using Spring. I would to like to have common entities which has to be shared by both MySQL and Cassandra I would like to change the DB dynamically based on customer preference. How can I achieve this
If your requirements to have the system runnable on both data stores, with only one at a time, this I think it is achievable.
The following advices i believe will let you achieve this but i didn't try'em: (supposing that you uses spring-boot)
In maven put the dependency for spring data jpa and spring data mongo.
Enable both jpa and mongo repositories (see EnableJpaRepositoies)
On entities put both JPA and Mongo annotations.
Use PagingAndSortingRepository as base interface for you repositories
In application.properties either configure mysql or mongo db through spring boot well-defined properties
This might work in regular spring apps but will need modifications; also include corresponding driver dependencies in pom file.
Edit: you can use 'spring.data.jpa.repositories.enabled=false' ( and its mongo counterpart) source: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java
I have done some research but I haven't come to a final conclusion yet.
What is the best way to connect to MySQL DB when having Spring MVC + tomcat?
Is it Hibernate, JPA, JDBC or Hibernate/JPA?
Thanks,
Sara.
JPA is more like a standard. Hibernate is an implementation of the JPA, which means Hibernate is one possible way of using JPA for database connection.
Eclipselink is an other popular implementation of JPA. (Earlier versions of Hibernate was not JPA, but you'll probably do not want to use pre-JPA Hibernate.)
So these would be the options:
Hibernate/JPA
Eclipselink/JPA
JDBC
JDBC is a low-level approach, it is much more troublesome to use it but also more felxible and sometimes you can achieve better performance with it. I'd drop JDBC until you have such problems that cannot be solved with JPA.
It is hard to choose between Hibernate and other JPA implementations unless you provide more details about your project. One thing is sure, Hibernate is popular (here on SO as well) and you'll be able to get help if you get stuck on a specific issue.
I suggest to go for Hibernate/JPA.
I have a simples question. I have been trying to learn Grails by my own, and i managed to do a simple application using Grails/Gorm.
1 ) Later, i decided to use Mysql instead of Gorm - i just needed to configure the 'DataSource' and download the driver.
2 )So if i want to use hibernate between both (Grails and MYSQL) like this:
http://www.grails.org/doc/latest/guide/15.%20Grails%20and%20Hibernate.html, i need to make an 'hibernate.cfg.xml' file, and specify my mysql database url, user, pw etc .. and i have to map each Class in Grails for MySql columns.
So what is the diference between 1) and 2) ? and what exactly hibernate does. Give examples if possible
PS. Please correct me if i said something wrong, im kinda new to this
I think you are a bit confused here.
GORM is not a database, it is a ORM that maps you Groovy classes to database tables. It uses Hibernate under the covers to achieve this (Hibernate is also an ORM).
The default database Grails uses is an in-memory HSQL DB. If you want to use MySQL instead of that, all you need to do is change the settings in conf/DataSource.groovy.
You don't need to create any Hibernate xml files. That part of the documentation you've linked to is to allow people with existing Hibernate domain models to easily re-use them.
Hope this helps clear things up.
cheers
Lee
i was asked to do a book manager at university with hibernate and mysql. I have a simple question. If i choose to do a web application, grails already uses hibernate. GORM runns over hibernate. so to use mysql i only need to configure jdbc grails drivers and that's it?
i mean, "for the project you must use hibernate and mysql" - this are the requirements. So can i do that way?
thanks in advance,
JM
Yes, of course you can.
You'll need to get the MySQL JDBC driver from this location.
Grails? When you're new to programming? Whose idea was this?
Personally, I think that taking on all these unknowns is risky for someone who's "new to programming." Do you know anything about SQL or JDBC? Have you ever written a web project before? This could be difficult.
I don't know how to be more specific. Download the JDBC JAR from the link I gave you.
I'd recommend that you start with a JDBC tutorial first. Hibernate is not for you - yet.
Hibernate is an object-relational mapping tool (ORM). It's a technology that lets you associate information in relational database tables to objects in your middle tier. So if you have a PERSON table with columns id, first, and last Hibernate will let you associate those data with the private data members in your Person Java class.
That sounds easy, but it gets complicated quickly. Your relational and object models might have one-to-many and many-to-many relationships; Hibernate can help with those. Lazy loading, caching, etc. can be managed by Hibernate.
But it comes at a cost. And it can be difficult if you aren't familiar with it.
If you have a deadline, I'd recommend creating a Java POJO interface for your persistence classes and doing the first implementation using JDBC. If you want to swap it out for Hibernate later on you can do it without affecting clients, but you'll have a chance of making progress without Hibernate that way.