there are some prerequisites to test the hibernate data access with SQL server.
- Hibernate Lib. http://www.hibernate.org/downloads
- SQL Server JDBC Driver 3.0 http://msdn.microsoft.com/en-us/sqlserver/aa937724
- some XML configurations ( hibernate.cfg.xml, POJO.hbm.xml)
I will provide some snippets as following,
Create a java project, add the jars to the builder path. 101 with Hibernate, first create one POJO ( a basic entity bean with “attributes”), I will create a basic Class called TODO with two attributes, ID and Title.
package Domain; public class TODO { private int ID; private String Title; public void setID(int iD) { public int getID() { public void setTitle(String title) { public String getTitle() { |
Then Add one XML mapping for this Class. (the file will used by hibernate to interpret the field-column mapping, ID generation rules.) , always using the naming rules as [ENtity].HBM.XML which is optional.
for the entitye TODO, will be mapped to a table called TODOs, the ID generation rule is implemented by SQL server( identity column)
<?xml version="1.0"?> <class table="TODOs" name="TODO"> <property name="Title"></property> </class> |
then create one file HIbernate.cfg.xml ( the driver used to communicate with sql server. )
<?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"> |
Then, read /write to and from DB using the hibernate API.
package test; import org.hibernate.Session; public class Main { /** SessionFactory factory = new Configuration().configure( Domain.TODO newobj = new Domain.TODO(); Domain.TODO newobj2 = new Domain.TODO(); java.util.List lists = session.createQuery("from TODO").list(); trans.commit(); } } |
When you run the program, it will create two records and save it to DB. here is the sql used captured by SQL profiler.
If you get the error when you run the app,
Jan 12, 2011 2:19:19 PM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path
that means the JDBC driver need loaded some DLL in order to work with SQL server. just copy this dll [located in the driver auth folder] to the class path folder. or just c:\windows folder.
this only applies if we use the Windows authentication against sql server instead of sql authentication.
<property name="connection.url">jdbc:sqlserver://localhost;databaseName=test;integratedSecurity=false; </property>
2 comments:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
feb 20, 2013 3:24:42 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
feb 20, 2013 3:24:57 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 08S01
feb 20, 2013 3:24:57 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
Any idea as to why i can get no connection going ?
MsSql Server 2008 server is running with tcp/ip enabled on client en server protocols
Please enable TCP port from
MSSQL Configuration tools
Post a Comment