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`")
Related
I'm getting Faculty table is not mapped in the query i am trying to execute but in hibernate.cfg.xml I have done mapping of Faculty
Project structure
Hinbernate.properties
# Hibernate Datasource
hibernate.connection.url=jdbc:mysql://localhost:3306/iiitb?createDatabaseIfNotExist=true&useSSL=false
hibernate.connection.username=pratham
hibernate.connection.password=prathamrathore
hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
# The SQL dialect makes Hibernate generate better SQL for the chosen database
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
# For Debugging
hibernate.show_sql=true
hibernate.connection.pool_size=100
# Hibernate ddl auto (create, create-drop, validate, update)
hibernate.hbm2ddl.auto=update
Hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/iiitb?createDatabaseIfNotExist=true&useSSL=false</property>
<property name="connection.username">pratham</property>
<property name="connection.password">prathamrathore</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.pool_size">100</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- DB schema will be updated if needed -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="org.iiitb.bean.Student"/>
<mapping class="org.iiitb.bean.Course"/>
<mapping class="org.iiitb.bean.Faculty"/>
<mapping class="org.iiitb.bean.CourseStudent"/>
</session-factory>
</hibernate-configuration>
FacultyDAO
here is the glassfish server log error
WELD-000119: Not generating any bean definitions from org.glassfish.jersey.media.multipart.internal.EntityPartWriter because of underlying class loading error: Type jakarta.ws.rs.core.EntityPart not found. If this is unexpected, enable DEBUG logging to see the full error.]]
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 have mysql DB with UTF-8 and application written in java with hibernate. When I run the application in eclipse everything is fine. But in production which is on a different machine the values returned from DB are corrupted.
I print the values to log (immediately after getting them) and I clearly see that the values I get from DB are corrupted in production.
The DB itself is the same DB for both environments. The values are stored fine.
Any ideas what can be the reason for this?
UPDATE:
I forgot to say that it happens sometimes. I think that in 50% of the times it works fine.
UPDATE 2:
Here is the 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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://myDB:3306/myApp?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<!-- <property name="hibernate.connection.pool_size">10</property> -->
<property name="hibernate.connection.isolation">2</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.provider_class">com.zaxxer.hikari.hibernate.HikariConnectionProvider</property>
<property name="hibernate.hikari.minimumIdle">5</property>
<property name="hibernate.hikari.maximumPoolSize">10</property>
<property name="hibernate.hikari.idleTimeout">30000</property>
<mapping class="someclass1"/>
<mapping class="someclass2"/>
<mapping class="someclass3"/>
<mapping class="someclass4"/>
</session-factory>
</hibernate-configuration>
Reference URL: https://docs.jboss.org/exojcr/1.12.13-GA/developer/en-US/html/ch-db-configuration-hibernate.html
Please verify the datasource configuration in your production environment whether the attributes - useUniCode and characterEncoding are set properly. Example:
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/exodb?relaxAutoCommit=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8"/>
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
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