Hibernate - Cannot create table in postgresql - java

I am trying to use hibernate framework with eclipse in order to build a simple web application and connect it with PostgreSQL. Here is my code:
Here is the error:
Does anyone know what I am doing wrong?

You should change property hibernate.hbmddl.auto value to create-drop or create to force hibernate create schema, because userclass table is not exists.
More information about hibernate.hbmddl.auto is here: https://stackoverflow.com/a/24417561/2055854

Just for those whom might encounter the same problem...
I just found the answer for it. Because of the database I am using is postgresql, I had to change the property "hibernate.dialect" in the persistence file. So, the result after the change was:
After that, the table was created!
Thank you.

Related

HSQL DB in Spring/Hibernate/JPA - unable to change schema

I am using HSQL in memory Db for unit testing Java code. Its configured and brought up using Hibernate/JPA/Spring
By default its schema is PUBLIC. However the code I am testing has hardcoded schema. So I need to change the schema from PUBLIC to say ABC
I just figure out how to do it. I tried below approaches
CRATE SCHEMA ABC, SET INITIAL SCHEMA ABC
ALTER CATALOG RENAME PUBLIC TO ABC
I get the error 'Schema not found'
That led me to conclude that once setup and started, we cant change the schema. So I tried with configuring schema as a Entity Manager JPA property at env setup. I started getting 'Dependent Bean not found' error for one of the classes in "domain.packages.to.scan"
I searched a lot on the web, I couldnt find definitive answers on how to accomplish this ?
The simplest way is to rename the PUBLIC schema. Just use the correct syntax from http://hsqldb.org/doc/2.0/guide/databaseobjects-chapt.html#dbc_renaming
ALTER SCHEMA PUBLIC RENAME TO ABC
You can always rename the schema or change to a different schema that you create.

How can I ask hibernate to make a Database for me? (maven & java)

I have University homework that asks me to create a simple database with hibernate, but I'm having a lot of troubles... I would like if someone would show me a solution.
I set the pom.xml with MySQL and hibernate dependencies; after that I made the hibernate.cfg.xml file in which I give it the connection URL. I am trying to do the annotated stuff so I don't have to do the other xml file... but it says that it can't find the database once I run the program, even if I specified in the cfg file that I have to create the database
<property name = "hibernate.hbm2ddl.auto">create</property>
Hibernate won't create the database for you by this property, only the tables.you can create it if it is not present already by adding a parameter to the URL.
jdbc:mysql://db:3306/mydb?createDatabaseIfNotExist=true

"Generate entities from table" (MS Access) in Eclipse via UCanAccess

I created a JPA project to use the tool Generate entities from Table to create an Entitie from a big big! MS Access Database Table.
I'm trying to use UCanAccess as JDBC Driver:
But when I test the connection I get this error:
Somebody knows what can I try to make it works?
Is there any other good tool to generate entities from table?
I have created a simple class to test UCanAccess and my database and everything works right.
It looks like there is something bad in your JDBC URL, e.g.:
jdbc:ucanaccess://C:/Users/Public/Database1.accdb;showSchema=true
Notice that the IDE can't know how the JDBC URL should be composed.
This seems to be working for me:

Hibernate database creation

I'm trying to create database schema on application server startup.
I use hebernate 4.1.9 with annotations and hibernate.cfg.xml for configuration.
So the problem is that I cann't fully understand what I should do to create schema and after that use it in application. Of course I want to perform schema creation only on first start and on the next start I want to update it.
I'm trying to use hbn2ddl.auto in update state, but database doesn't creates. May be I should use somethin like INIT=create schema IF NOT EXISTS myschema in the end of the hibernate.connection.url??
Also I have an exception
org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
But in hibernate.cfg.xml there is such string:
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
Can anybody describe it to me?
I solve the problem by myself. I should add createDatabase=true to hibernate.connection.url

Hibernate database name change gives MySQLSyntaxErrorException: Table doesn't exist

I used to have a database called database and everything was working well using hibernate and its models.
I removed <property name="hibernate.hbm2ddl.auto"> to avoid update or create as it's a production server, we want to do it manually.
We recently switched to database2 and so we switched the hibernate configuration file and all the hibernate XML models.
`<class name="com.api.models.database.MmApplications" table="mm_applications" catalog="database2">`
but it keeps looking for database event if we migrated the database, the models and the connexion.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'database.mm_applications' doesn't exist
Does someone can help me ?
UPDATE ----
Hibernate is connecting to the right database (database2), but there is a prefix as a prefix database. making the queries hitting the database instead of database2, and when I try to force the default_schema my queries become :
`... from database.database2.mm_applications ....`
Any idea?
My database is specified in the hibernate.connection.url property. Have you changed that also ? An example would be: jdbc:mysql://localhost/mydatabase
Also, instead of removing hibernate.hbm2ddl.auto then perhaps you should set its value to validate. That way hibernate will ensure that the datamodel matches the database schema.
I found the problem, It was an other application deployed on the same tomcat server using hibernate as well with another database (database) making a conflict with the new application ...
There is still something weird, by connecting to any database, hibernate will use the specified catalog in the hibernate models and so constructing the request using the catalog.table_name
Hope this help someone someday.

Categories