Bonjour,
Je suis debutant hibernate .
j'ai eu un probleme suivant au niveau de la connexion a sqlserver .
09:36:04,359 WARN SettingsFactory:109 - Could not obtain connection metadata
java.sql.SQLException: Connection refused: connect
at net.sourceforge.jtds.jdbc.TdsUtil.getSQLException(TdsUtil.java:181)
at net.sourceforge.jtds.jdbc.TdsUtil.getSQLException(TdsUtil.java:160)
at net.sourceforge.jtds.jdbc.TdsConnection.<init>(TdsConnection.java:182)
at net.sourceforge.jtds.jdbc.TdsConnectionJDBC3.<init>(TdsConnectionJDBC3.java:53)
at net.sourceforge.jtds.jdbc.Driver.getConnection(Driver.java:102)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:85)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:76)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1933)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1216)
at Exec.main(Exec.java:17)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at net.sourceforge.jtds.jdbc.TdsSocket.<init>(TdsSocket.java:182)
at net.sourceforge.jtds.jdbc.TdsConnection.<init>(TdsConnection.java:178)
... 10 more
09:36:04,593 WARN SessionFactoryObjectFactory:98 - Could not bind factory to JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52)
at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1218)
at Exec.main(Exec.java:17)
2
09:36:05,562 WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 08004
09:36:05,562 ERROR JDBCExceptionReporter:72 - Connection refused: connect
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at Exec.main(Exec.java:20)
Caused by: java.sql.SQLException: Connection refused: connect
at net.sourceforge.jtds.jdbc.TdsUtil.getSQLException(TdsUtil.java:181)
at net.sourceforge.jtds.jdbc.TdsUtil.getSQLException(TdsUtil.java:160)
at net.sourceforge.jtds.jdbc.TdsConnection.<init>(TdsConnection.java:182)
at net.sourceforge.jtds.jdbc.TdsConnectionJDBC3.<init>(TdsConnectionJDBC3.java:53)
at net.sourceforge.jtds.jdbc.Driver.getConnection(Driver.java:102)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:85)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 5 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at net.sourceforge.jtds.jdbc.TdsSocket.<init>(TdsSocket.java:182)
at net.sourceforge.jtds.jdbc.TdsConnection.<init>(TdsConnection.java:178)
... 12 more
Voici mon fichier 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 name="">
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433/testhib</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
</session-factory>
</hibernate-configuration>
voici ma class :
package appliTest ;
public class Test {
private Long id ; // type référence préférable car pointeur null
private String nom ;
private int nbRayons ;
public Test() {
this.id = null ;
this.nom = null ;
this.nbRayons = 0 ;
}
public Test(String nom) {
this.id = null ; // affecté par Hibernate, voir Responsable.hbm.xml
this.nom = nom ;
this.nbRayons = 0 ;
}
void setId (Long id) { this.id = id ;}
public Long getId () {return id ;}
public void setNom (String nom) { this.nom = nom ;}
public String getNom () { return nom ;}
public void setNbRayons (int nbRayons) { this.nbRayons = nbRayons ;}
public int getNbRayons () { return nbRayons ;}
public void addNbRayons (int n) { this.nbRayons += n ;}
}
a l execution sa bloque au niveau :
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Si quelqu'un sait pourquoi n'hésitez pas.
Merci d'avance
Partager