Database did not create with annotation on entity class - java

I created my entity classes and annotations normally, but hibernate can not generate the tables in my database.
##DataSource settings
spring.datasource.url=jdbc:oracle:thin:#//localhost:1521/BD_GESTION_OPERATION?createDatabaseIfNotExist=true&userSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=system
spring.datasource.password=SeCret
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
##Disabling spring basic security
security.basic.enabled=false
##Start up port
server.port=8082
##Specify DBMS
spring.jpa.database=ORACLE
##Show/Hide SQL queries
spring.jpa.show-sql=false
##Hibernate DDL Auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=update
##Naming strategy
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
##Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

Delete old tables. In your application.properties, set property like that (this is default, even you no need declaring it)
spring.jpa.hibernate.ddl-auto=create-drop
You also want to try
spring.jpa.hibernate.ddl-auto=create
Reference https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-initialize-a-database-using-hibernate

Related

Spring boot parameter update model Database don't work

i set the value to update value so that a table will be created in the database automatically corresponding to defined data model.
But it does not work, what it wrong with my properties ?
Database: Mysql
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB
server.port=8081
server.servlet.session.timeout=1200
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
spring.datasource.username= root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.storage.storage_engine=innodb
spring.security.user.name="root"
spring.security.user.password="123"
spring.resources.add-mappings=true
java.sql.SQLSyntaxErrorException: Table 'test.files' doesn't exist
What genereted conflict with "spring.jpa.hibernate.ddl-auto=update"
Make sure the database connection string is valid.
Try changing spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
The name of your database is "test" but is it really?

Spring Data JPA, Hibernate #Table

I have a problem:
My application has an MSSQL host in which there are 2 databases.
All my entities look at base number:
And one Entity should look at base
I can do this by specifying the base in the #Table parameter (schema = databasename.schema)
Is there any way to make the #Table (schema) parameter come from application.properties?
Yes, it is possible. Look at the following properties:
<property name="hibernate.default_catalog" value="crm"/>
<property name="hibernate.default_schema" value="analytics"/>
You can use the following properties in the spring boot application.properties:
spring.jpa.properties.hibernate.default_catalog=crm
spring.jpa.properties.hibernate.default_schema=analytics
Look also at the #Table annotation documentation. When you set the schema attribute hibernate uses this specified schema, otherwise it is used user default schema that you can provide via hibernate.default_schema.
Also you can try to write your custom schema name resolver. As it stated in the documentation for the hibernate.schema_name_resolver property:
By default, Hibernate uses the org.hibernate.dialect.Dialect#getSchemaNameResolver. You can customize how the schema name is resolved by providing a custom implementation of the SchemaNameResolver interface.

Is it possible to set hibernate.show_sql for Query programmatically

I use Hibernate with Spring and initialize it with LocalContainerEntityManagerFactoryBean bean with hibernate.show_sql=false in JpaProperties of it. So, I disabled SQL output of Hibernate, but I want to print SQL only for some Query, that I want to debug.
Is it possible to set hibernate.show_sql for Query programmatically, to don't print all SQL queries at runtime, but print them only for some Query?
And no, it's not a duplicate. Because, everything, that explained in suggested answer is already done in my app. I need to enable SQL logging not for all Queries in my app, but only for one Query.

How to configure JPA at run time(specialy schema of tables)

I am using JPA and I use Entity XML mapping Files to config my persistence layer on the other hand I have multiple database schemas in an Oracle database
In the persistence configuration, i want to have only one EntityManagerFactory and in fact i want to have connection with database with only one schema(USER).
Statically, I can specify schema name one by one to my Entities in Mapping files like this:
<entity class="package.MyClass" name="MyClass">
<table name="MYTABLE" schema="mySchema"/>
My persistence provider in this project is: org.hibernate.ejb.HibernatePersistence
I want to use a placeholder for specifying schema and replace that when JPA scan mapping files to configure itself.
<entity class="package.MyClass" name="MyClass">
<table name="MYTABLE" schema="#placeholder"/>
at config time replace #placeholder with mySchema
Is anyway to do this?
Which part (class) in JPA scan XML mapping files and can i customize that class to do this replace for me?
I think you can use the bootstrap procedure to access the EntityManagerFactory and the EntityManager at runtime to override the properties.

JPA persistence.xml flushmode with Hibernate 3.0

I understand that Hibernate uses transparent write behind by default for committing the transactions.
However, I would like my entity-manager to commit my transaction on the database immediately after the transaction is committed. Is there anyway I can configure this in persistence.xml of JPA ?
Hibernate would have to commit in the database at the time is made commits the transaction. You can also find it helpful to have two additional options:
Define the Session and autocommit (entering the property "hibernate.connection.autocommit" in the properties of the connection)
Forcing hibernate transaction synchronize with the database transaction in the middle of the transaction (by session.flush ())
Regards,

Categories