Hibernate configuration file - java

It seems that I have a problem with my configuration file but I really don't find out what it could be.
I used to use hibernate 3.6, I now use hibernate 4.2, there are proly some important changes that I ignore, but the doc seems to not change this part.
Here is my configuration file:
<?xml version='1.0' encoding='utf-8'?>
<!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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/Test</property>
<property name="connection.username">root</property>
<property name="connection.password">*****</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<property name="maxActive" value="10" />
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</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>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Mapping files -->
<mapping resource="database/config/mapping/test.hbm.xml"/>
</session-factory>
</hibernate-configuration>
And here is my java source:
public class CreateDB {
public static void main(String argv[]){
try {
Configuration configuration = new Configuration();
configuration.configure("database/config/DBCreate.cfg.xml");
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration
.getProperties());
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
DBConnect.Disconnect(null, sessionFactory.openSession()); // Just logout from a session
} catch (Exception e) {
System.out.println("Database unreachable.");
}
}
}
And here is the return statement:
mai 02, 2013 11:41:59 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
mai 02, 2013 11:41:59 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.0.Final}
mai 02, 2013 11:41:59 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
mai 02, 2013 11:41:59 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
mai 02, 2013 11:41:59 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: database/config/DBCreate.cfg.xml
mai 02, 2013 11:41:59 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: database/config/DBCreate.cfg.xml
Database unreachable.
I really don't get what is wrong, maybe someone will be able to enlight me.
Sorry for possible disturbance and if my post isn't adapted, this is my first one.
Best regards.

Specify the port in the connection.url.
<property name="connection.url">jdbc:mysql://localhost:3306/Test</property>

I have read an article which says in Hibernate 4 we require to mention the "hibernate" keyword in the property name tag of the configuration file.
Sample format is -
Hibernate earlier version
<?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/noob</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- DB schema will be updated if needed -->
<property name="hbm2ddl.auto">create-drop</property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
</session-factory>
</hibernate-configuration>
Hibernate version 4
<hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-
configuration">
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/noob</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.connection.pool_size">1</property>
<!-- SQL dialect -->
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property
name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Observe the "hibernate" keyword in the property name tag
The exact link is as follows -
hibernate.cfg.xml with Hibernate 4.0.0

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
When you change the XML file as per above comments, you must change the DTD definition.

Related

Why is Faculty table not mapped?

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.]]

Hibernate configuration error while reading schema

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

Hibernate Configration file error

I am new to hibernate framework. i made a sample project just to test hibernate. i set up the project. but when i run it i got this error
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966)
The Hibernate.cfg.xml file that i put in the main package of the project.
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<property name="hibernate.dialect">
org.hibernate.dialect.DerbyDialect
</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">2</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--<property name="htm2ddl.auto">create</property>-->
<property name="hibernate.connection.driver_class">
org.apache.derby.jdbc.ClientDriver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:derby://localhost:1527/NetworkDB
</property>
<property name="hibernate.connection.username">
</property>
<property name="hibernate.connection.password">
</property>
<!-- List of XML mapping files -->
<!-- <mapping resource="Employee.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
I am using netbeans 7.0
As per DTD, <hibernate-configuration> should have a single <session-factory> but you have declared it twice.
Regarding second error:
org.hibernate.HibernateException: /hibernate.cfg.xml not found
Hibernate looks for the configuration file at the root of the classpath, so check if you have placed this file at root of your classpath.
So once you build your project the hibernate.cfg.xml file should be in classes directory.
Add the following in first line of your xml:
<?xml version="1.0" encoding="UTF-8"?>

Hibernate with MS SQL

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`")

Hibernate Mapping Files Not Found(MappingNotFoundException)

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

Categories