Hey stackoverflow,
I tried to add a Hibernate configuration to my workspace in order to reverse engineer my database.
When I am opening the database point in the 'Hibernate Configurations' view, i get a Reading schema error: null with the following exception:
java.lang.NullPointerException
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$1.compare(LazyDatabaseSchemaWorkbenchAdapter.java:76)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.toArray(BasicWorkbenchAdapter.java:75)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:74)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:104)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:238)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Has someone experienced this before?
I am running a fresh installation of Eclipse Luna / Newest JBoss Tools and trying to connect to a PostgreSQL 9.3 DB.
Thank you in advance
EDIT:
My hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">jdbc:postgresql://10.244.7.77:5432/netview</property>
<property name="hibernate.connection.username">admin</property>
<property name="hibernate.default_schema">public</property>
</session-factory>
</hibernate-configuration>
EDIT 2:
I just realized, if i add a default schema <property name="hibernate.default_schema"> it works, but just for this schema.
EDIT 3:
It works with an old version of Hibernate tools (3.6.0.M1-v20120827-0757-H1125). Now i am confused.
Try out this config:
<!-- Database connection settings -->
org.postgresql.Driver
jdbc:postgresql://host:port/database
postgres
password
1
org.hibernate.dialect.PostgreSQLDialect
thread
org.hibernate.cache.internal.NoCacheProvider
true
Try out this config:
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://host:port/database</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
</session-factory>
So i found out that it might be a bug in JBoss Tools. So I opened a ticket: https://issues.jboss.org/plugins/servlet/mobile#issue/JBIDE-19830
Related
I have this hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">
jdbc:postgresql://localhost/DatabaseName?createDatabaseIfNotExist=true
&useUnicode=yes&characterEncoding=UTF-8
</property>
<property name="connection.username">postgres</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.main.User"/>
</session-factory>
</hibernate-configuration>
which is supposed to create both tables (for the User entity and the database DatabaseName). However, it doesn't create a database and fails with an error on this line:
sessionFactory = configuration.buildSessionFactory();
What can I do to make it autocreate database titled DatabaseName?
To create database you have to create is manually you can use IDEs for that or cmd to create your DB
And To create Tables you can use create in place of update
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
It will create a schema and then you will use an update for the next startup if you don`t want to recreate your tables
I'm trying to reverse Engeneer my simple MYSQL DB using Hibernate Configuration with eclipse IDE.But getting below error.
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/client1_db
I went through similar issues. This is a Marven Web Project and I can see the MYSQL_connector_java_5.1.34.jar is there under Marven Dependencies library. Some had suggested "No suitable driver found" means the drivers are loading but an issue with the DB connection. I have tried to connect to the localhost and the DB was created using MySQL workbench and it is accessible. I even tried using myphp admin and the DB is accessible.
hibernate.cfg.xml file
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bookshop</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop the existing tables and create new one -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Then I tried using different server port 3307 as below. Still the same error.
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3307/client1_db</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
The full Error Message
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/client1_db
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getConnection(AbstractMetaDataDialect.java:121)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getMetaData(AbstractMetaDataDialect.java:60)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.caseForSearch(AbstractMetaDataDialect.java:163)
at org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect.getTables(JDBCMetaDataDialect.java:22)
at org.hibernate.cfg.reveng.JDBCReader.processTables(JDBCReader.java:476)
at org.hibernate.cfg.reveng.JDBCReader.readDatabaseSchema(JDBCReader.java:74)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.tools.hibernate.runtime.common.Util.invokeMethod(Util.java:43)
at org.jboss.tools.hibernate.runtime.common.AbstractJDBCReaderFacade.readDatabaseSchema(AbstractJDBCReaderFacade.java:44)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$2.execute(LazyDatabaseSchemaWorkbenchAdapter.java:132)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:107)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.readDatabaseSchema(LazyDatabaseSchemaWorkbenchAdapter.java:124)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:64)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:104)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:219)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Hibernate Configuration screenshot
Package Explore
Please visit the below page :
https://javarevisited.blogspot.com/2016/09/javasqlsqlexception-no-suitable-driver-mysql-jdbc-localhost.html
Instead of doing the reverse engineering directly I first added the Database to the Eclipse providing JDBC driver path. After testing the connection was successful then performed the reverse engineering and it was successful.
Steps >
01. Go to Data Source Explore
Add the DB connection
Make sure you map the JDBC drivers
04.Test at the end. Make sure your server is running..
Need some clarification and helps. Especially appreciate describing general concepts or link where they are described.
So, on the hibernate website I have read the next one:
For use inside an application server, you should almost always
configure Hibernate to obtain connections from an application server
javax.sql.Datasource registered in JNDI. You will need to set at least
one of the following properties:
And I have a few question because at the moment I am really confused about all of that stuff with DataSource, DataDriver, Tomcat and Hibernate in general.
Does configuring Datasource and binding SessionFactory to the JNDI
is the same process?
If no, for what we use DataSource and for why we need to bind SessionFactory to JNDI (in general)?
Am I understood right? If we configure DataSource in hibernate.cfg.xml file we don't need to configure it in {tomcat}/conf/server.xml or {tomcat}/conf/context.xml?
What is hibernate.jndi.url? Does it is the same as hibernate.connection.url?
What is hibernate.connection.datasource? In docs I read that it is "datasource JNDI name", so if I understood right it can be any name?
From Hibernate docs I read that setting at least one of the properties hibernate.connection.datasource, hibernate.jndi.url, hibernate.jndi.class, hibernate.connection.username, hibernate.connection.password makes my app use javax.sql.Datasource registered in JNDI. So does the next conf already configured to use DataSource?
How to check that DataSource used and configured fine?
My hibernate.cfg.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.generate_statistics">true</property>
<!--http://stackoverflow.com/questions/2067526/hibernate-connection-pool-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--For use inside an application server, you should almost always configure Hibernate to obtain connections from an application server javax.sql.Datasource registered in JNDI. You will need to set at least one of the following properties:-->
<!--hibernate.connection.datasource,hibernate.jndi.url,hibernate.jndi.class,hibernate.connection.username,hibernate.connection.password-->
<!--Datasource config-->
<property name="hibernate.connection.datasource">jdbc:mysql://localhost/easywordweb</property>
<!--<property name="hibernate.jndi.url">??????? what is it</property>-->
<!--/Datasource config-->
<!--*****************************************************************-->
<!--C3P0 config-->
<!--Hibernate will obtain and pool connections using java.sql.DriverManager if you set the 5 following properties -->
<!--hibernate.connection.driver_class,hibernate.connection.url,hibernate.connection.username,hibernate.connection.password,hibernate.connection.pool_size-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/easywordweb</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<!--We can use a third party pool for best performance and stability, for example c3p0. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use c3p0. -->
<!--<property name="hibernate.connection.pool_size">140</property>-->
<property name="hibernate.c3p0.max_size">140</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<!--max to cache-->
<property name="hibernate.c3p0.max_statements">50</property>
<!--The seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire. Hibernate default: 0-->
<property name="hibernate.c3p0.timeout">21600</property>
<!--for test, change futher-->
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>
<!--at every connection checkin to verify that the connection is valid-->
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<!--at every connection checkout to verify that the connection is valid-->
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<!--/for test, change futher-->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--/C3P0 config-->
<!--*****************************************************************-->
<property name="hibernate.c3p0.validate">true</property>
<!--c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds-->
<property name="hibernate.c3p0.idle_test_period">21000</property>
<property name="hibernate.jdbc.batch_size">20</property>
<!--Number rows to be returned if no setted-->
<property name="hibernate.jdbc.fetch_size">20</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<!--FIXING: Table "...".hibernate_sequence table not found.-->
<property name="hibernate.id.new_generator_mappings">false</property>
</session-factory>
</hibernate-configuration>
Thank's everyone in advance.
In the config you have posted you are initializing the connection pool within your application.
An alternative is to delegate the creation of the database pool to your app/web server and expose it as a JNDI resource. Your application need then only specify the name of the JNDI datasource to obtain a connection.
Doing this in Tomcat is documented here:
https://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
Your hibernate.cfg.xml then looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- The Server configured JNDI datasource -->
<property name="hibernate.connection.datasource">java:comp/env/jdbc/MyLocalDB</property>
<property name="hibernate.jdbc.batch_size">20</property>
<!--Number rows to be returned if no setted-->
<property name="hibernate.jdbc.fetch_size">20</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<!--FIXING: Table "...".hibernate_sequence table not found.-->
<property name="hibernate.id.new_generator_mappings">false</property>
</session-factory>
</hibernate-configuration>
I'm trying to integrate hibernate with MS SQL, below is the sql query I get from hibernate
12:27:44,172 DEBUG [AbstractSaveEventListener] Executing identity-insert immediately
Hibernate:
insert
into
aide.dbo.rule
(appId, ruleName)
values
(?, ?)
causes error
12:27:44,229 DEBUG [SqlExceptionHelper] Incorrect syntax near the keyword 'rule'. [n/a]
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'rule'.
same error is thrown in MS SQL management studio too
while this command runs fine
insert
into
[aide].[dbo].[rule]
(appId, ruleName)
values
('rf', 'wfw')
below is my hibernate cfg
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- <property name="hibernate.bytecode.use_reflection_optimizer">false</property> -->
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433</property>
<property name="hibernate.default_catalog">aide</property>
<property name="hibernate.default_schema">dbo</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.username">aide</property>
<property name="hibernate.connection.password">aide</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="hibernate.show_sql">true</property><!-- JDBC connection pool (use the
built-in) -->
<property name="hibernate.connection.pool_size">1</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.****.Rule" />
<!-- <mapping resource="com/****/Rules.hbm.xml"></mapping> -->
</session-factory>
</hibernate-configuration>
(sqljdbc4.jar downloaded from microsoft website)
seems hibernate is generating a query not understandable by MS SQL
RULE is a SQL Server reserved keyword.
If you need to stick to that name you need to escape it with:
#Table(name="`rule`")
I followed this tutorial: http://manikandanmv.wordpress.com/2011/04/13/hibernate-basics-simple-example/
This is my file structure
http://i.stack.imgur.com/sX3nn.jpg
When I run Java Applet, I get this error
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: src/com/bookstore/bookapp.hbm.xml not found
However, I have that file there as you can see it. When I put the file under
workspace-windows\226project1\src\com\bookstore
I'm still facing this error even then, Can someone help?
This is my hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">postgres</property>
<property name="connection.password">rocker123</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="src/com/bookstore/bookapp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Move your mapping files into src/main/resources. Only .java files belong in src/main/java.
There is an example project on github showing how to correctly reference XML mapping files from a hibernate.cfg.xml. Please refer to https://github.com/zzantozz/testbed/tree/master/hibernate-with-xml-mappings