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

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.

Related

Is the mysql connector needed when I am trying to connect using JPA?

I have been fetching data from database to my JAVA program by using JDBC till now and I am doing so by creating a connection class and in that class I will use the DriverManager.getConnection() fnc, but as I am moving towards JPA I have learnt that a persistence.xml file is needed to connect to mysql. In the project where I am using JPA am i supposed to create this connection class again and should I place the connector jar file in the project where I will be using jpa.
Excuse my unawarness of the concept I am still in the learning phase.Any help is appreciated as I am new to this.
Here Need to clear for you.
JPA is a specification. Different ORM technology uses it. Like hibernate implemented JPA specification. Specification defines how it works.
Hibernate is ORM technology. It binds Your Plain Java Object (Entity) to your database tables. And table's column will be Entity's field. Like Table has Id column of number type, in entity it will be Long id; . table name will be entity name or others that is defined in hibernate docs.
3rd One is Database connector. Yes there is different types of connector for every database. like for mysql connector is using to connect your implemented code with databases. You can think Its a communication Layer for database and your code. Your code is communicating with database through this connector.
Hope now you get concept why mysql connector is needed to connect. Happy Coding :)

Hibernate - Cannot create table in postgresql

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.

How to update tables from back end that it reflects in retrieved entities

I'm developing a Spring MVC web application using Windows 7, Eclipse Juno, Eclipselink JPA as ORM and Glassfish as application server with Oracle 11g. While I was working with Eclipselink I noticed when I update a table manually by execute an update PL/SQL query it doesn't has any affect on entities that already retrieved by Eclipselink until restart the server. Although, I disabled Eclipselink cache by having <shared-cache-mode>NONE</shared-cache-mode> in persistance.xml and using EntityManager.clear(), EntityManager.close() and #Cacheable(false).
Then, I noticed when I update tables using Oracle-SQLDeveloper table designer it totally works fine and entities are showing updated information. So I checked SQLDeveloper log to see what query it's using to update rows and I saw that it's using ORA_ROWSCN and ROW ROWID in where clause. After that, I exactly used the same where clause as the one SQLDeveloper used to update tables, but still entities were showing old information.
I'm wondering what factors are involved here that Eclipslink is not fetching real time data from database ? but, after updating table with SQLDeveloper designer Eclipselink is showing real time data. It seems that modifying a table data with SQLDeveloper table designer also marks the table as changed by using some database features. Then, Eclipselink will read the mark before hitting the table.
Also to get more clarification, anyone knows what steps are involved in Eclipselink before it decides to hit database when user commands to execute a TypedQuery ? I'm so curious that where it stores cached entities ? since cache rest just by restarting the computer; I tried restarting Glassfish, killing the java process and logoff current user, but none of them worked. Why Eclipselink still is caching entities since I configured it to not use any caching procedure? Is it possible to completely turn off cache in Eclipselink?

Play! JPA: Enforce not to create Entity on load

I am connecting to an external database (i.e. Oracle 10g) using my Play 1.2.4 application.
I have provided all the DB details in the application.conf file, then added the ojdbc.jar in the classpath and the connection seems to work fine. But the problem is everytime when the application access some data for the first time. it is trying to create the entity table (which is already present) and hence throws an exception.
I don't want to create any table since its already there with some data, hence how can I avoid this?
There might be some configuration to set in the application.conf for accessing the external database where the table is already available(I believe), kindly help me on this.
Below is the conf file:
application.mode=dev
%prod.application.mode=prod
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=****
db.pass=****
#%test.module.cobertura=${play.path}/modules/cobertura
%test.application.mode=dev
%test.db.url=jdbc:h2:mem:play;MODE=MYSQL;LOCK_MODE=0
%test.jpa.ddl=create
%test.mail.smtp=mock
Note: Presently I am connecting to the external Oracle database and not with the embedded H2 database.
In your application.conf set the following property to none:
jpa.ddl=none

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

Categories