I am building a spring MVC web application. I am using Hibernate as my ORM. Whenever I restart my tomcat 7 server, it automatically deletes the table created previously. How to prevent this?
Search for hbm2ddl text somewhere in your configuration, close to Hibernate configuration properties. It should be set to update or none, apparently you have create-drop.
See also
Hibernate hbm2ddl.auto possible values and what they do?
Related
What is the difference between spring.jpa.hibernate.hbm2ddl and spring.jpa.hibernate.ddl?
I have found in this question: What are the possible values of spring.datasource.initialization-mode? that OP is using both in properties, however it seems like the origin of hbm2ddl is hibernate directly not Spring Data Jpa.
Nevertheless, reading the answer from another OP, it looks like pass-through only.
However in our commercial project with mariadb, when we do not close our spring boot application gracefully with spring.jpa.hibernate.hbm2ddl.auto=create, when the application is run again, it deletes old data and creates everything from scratch. On the other hand with spring.jpa.hibernate.ddl.auto=create every second run (after no graceful application shutdown) causes key constraint exceptions (DB is not being dropper before creation)
From this Link
By default, JPA databases are automatically created only if you use an embedded database (H2, HSQL, or Derby).
You can explicitly configure JPA settings by using spring.jpa.* properties. For example, to create and drop tables you can add the following line to your application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
Hibernate’s own internal property name for this (if you happen to remember it better) is hibernate.hbm2ddl.auto.
From this Link
spring.jpa.hibernate.ddl-auto This is actually a shortcut for the "hibernate.hbm2ddl.auto" property.
Defaults to "create-drop" when using an embedded database and no schema manager was detected. Otherwise, defaults to "none".
From this Link
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts).
It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively.
In addition, Spring Boot processes the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform.
This allows you to switch to database-specific scripts if necessary. For example, you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql, and so on).
My database.properties file is:
datasource.driver=org.h2.Driver
datasource.url=jdbc:h2:file:./test_database/comixed_db;create=true
datasource.username=sa
datasource.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.batch.size=20
hibernate.current.session.context.class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.generate_statistics=false
hibernate.enable_lazy_load_no_trans=false
When my application starts up I see I am able to see the tables by using the h2.bat tool and peeking at the database from a web browser. However, when I shut down the application and then go back to the database with the h2.bat tool the tables are all gone!
AM I doing something incorrectly with my hibernate configuration? I am not using create-drop but update since this code is currently in flux and I'd like the tables to be adjusted as changes occur. But that doesn't seem to be the issue since it's at app shutdown that the tables keep going away.
Any help would be appreciated.
If you want spring boot to catch your hibernate properties you should prefix them with spring.jpa, so:
spring.jpa.hibernate.ddl-auto=update
Otherwise, and that is the case in my opinion, spring will use the default create-drop options as it is dealing with an H2 in-memory database.
By adding the following line to applications.properties:
spring.jpa.hibernate.ddl-auto=update
Spring-boot stopped dropping tables when the application exits.
I have a Google Cloud Dataflow application that as part of a transform records some information to a MySQL database through JPA 2 and Hibernate. When running this application with the DirectPipelineRunner it locates the persistence.xml and runs fine.
However, when I try to make use of the BlockingDataflowPipelineRunner the persistence context won't initialize because it can't find the persistence.xml. Maybe it can't be loaded from the CLASSPATH? The message being that the only persistence unit named in the persistence.xml isn't found.
Has anyone successfully used JPA 2 and Hibernate with a Dataflow application? If so, what hoops did you have to jump through to get it working when using the BlockingDataflowPipelineRunner?
Hibernate by default looks for the persistence.xml file in potentially many locations. You may want to confirm that the jars that get uploaded contain META-INF/persistence.xml.
If you add the flag
--workerLogLevelOverrides=org.hibernate#TRACE
, that should give you much more detailed output related to Hibernate (you may need to configure JBoss logging to go to SLF4J or JUL to get the Hibernate logging to appear, docs.jboss.org/hibernate/orm/4.3/topical/html/logging/…).
I'm trying to integrate Hibernate into my Netbeans project to connect to Microsoft SQL Server. I've run the Hibernate Configuration file wizard (and set up a new database connection). I've set the dialect to SqlServerDialect. I initially set up the connection with the db_owner schema. I've run test queries on the connection and they all return the correct information.
However, when I try to run either the Hibernate Mapping wizard or the Hibernate Reverse Engineering wizard, there are no available tables to select.
I'm using the JDBC from Microsoft (version 4). There are other similar questions already posted, but no answers that have been provided work.
It turns out I selected the wrong default schema. I selected db_owner and it should have been dbo. When I checked the connection under the services tab and expanded the default schema, it showed no tables. Apparently the SQL executor uses other schemas besides the default to find results.
I have a struts 2 application and a toplink persistence provider running on tomcat 6.0.20 and a MySql 5.1.38 server on a GNU/Linux machine. After committing the data the when i go to retrieve it the data it has disappeared from the database.
I do a em.commit() and em.flush() after my queries have executed. How do they disappear? I am using all standard configuration files. I have reduced the wait_timeout and the interactive_timout period in mysql. Also am using autoReconnectforPools in my persistence.xml.
I also invalidate the cache on every users logout.
Any ideas?
anyway it does not matter, the problem was solved by removing softweak from persistence.xml's entity type declaration and adding hardweak in its place.