Problems converting XML to Java when using hibernate


#1

Hello,

So I am having issues trying to convert XML into Java for a connection. Below is my code. I was wondering if anyone could help me figure it out.

Here is the XML FIle.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">



<hibernate-configuration>
	<session-factory>
		<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</property>
		<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
		<property name="hibernate.connection.url">jdbc:sqlserver://sedb.database.windows.net:1433; </property>
		<property name="hibernate.connection.database">test1</property>
		<property name="hibernate.connection.username">test@test</property>
		<property name="hibernate.connection.password">123456789</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
DB
		<mapping class="main.S"/>
		<mapping class="main.C"/>
		<mapping class="main.D"/>
		<mapping class="main.P"/>
		<mapping class="main.E"/>
	</session-factory>
</hibernate-configuration>

Here is my attempt at converting.

	try
		{
			String host = "jdbc:sqlserver://sedb.database.windows.net:1433/test1";
			String uName = "test@test";
			String uPass = "123456789";
			Connection con = DriverManager.getConnection(host,uName,uPass);
		}
		catch (Exception err)
		{
			System.out.println("Error");
		}

As you can see Im quite new to this. Any pointers would help.

Thanks!


#2

So, what is it that you are trying to convert from XML to Java?

The second code snippet is just a JDBC connection acquisition code. The question is why you’d want to do that when you can have Hibernate do that for you?


#3

I am trying to add the xml code inside my main java class file. Is there a way hibernate can do that for me?


#4

Check out the Getting Started Guide.


#5

Thanks. I found out the real issue is trying to run the. Java file. I’m getting "error: package javax. Persistence does not exist.

I tried adding this in POM but no luck

org.hibernate hibernate-core 4.3.9.Final

#6

Also added this

javax.persistence persistence-api 1.0.2

#7

Don’t use these older versions of Hibernate and JPA. Use the latest ones.

And use Maven for dependency management. Just read the Hibernate docs and you’ll know what to do.