2017-10-20 16 views
0

Je suis très nouveau à Spring, Hibernate. tout en travaillant sur hiberner je suis confronté au problème suivant. J'ai également recherché des tags associés sur stackoverflow, mais je n'ai trouvé aucun article pertinent ayant résolu mes problèmes.Exception dans la configuration d'Hibernate

Student.java fichier

package hibernatepractise; 

    public class Student { 
     private long id; 
     private String name; 
     private String degree; 
     private String phone; 



     public Student() { 
      super(); 
     } 

     public long getId() { 
      return id; 
     } 

     public String getName() { 
      return name; 
     } 

     public String getDegree() { 
      return degree; 
     } 

     public String getPhone() { 
      return phone; 
     } 

     public void setId(long String) { 
      id = String; 
     } 

     public void setName(String string) { 
      name = string; 
     } 

     public void setDegree(String string) { 
      degree = string; 
     } 

     public void setPhone(String string) { 
      phone = string; 
     } 

     public String toString() { 
      return name; 
     } 

} 

AddStudent.java Fichier

package hibernatepractise; 


import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.Session; 
import org.hibernate.cfg.Configuration; 

import hibernatepractise.Student; 

public class AddStudent { 
    private static SessionFactory sessionFactory; 

    public static void main(String args[]) throws Exception { 
     // begin if 
                    // A 
      String name = "Jayesh Vyas"; 
      String degree = "B.tech Completed"; 
      String phone = "9421345678"; 

      System.out.println("Name: " + name); 
      System.out.println("Degree: " + degree); 
      System.out.println("Phone: " + phone); 

      if ((name.equals("") || degree.equals("") || phone.equals(""))) { 
       System.out.println("All informations are Required"); 
      } else { 

       try {// begin try 
        sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory(); 
        // sessionFactory1 = new 
        // Configuration().configure("com\\xml\\student1.cfg.xml").buildSessionFactory(); 
       } catch (Exception e) { 
        System.out.println("mathan"); 
        System.out.println(e.getMessage()); 
        System.err.println("Initial SessionFactory creation failed."+ e); 

       } 
       Session s = sessionFactory.openSession(); 
       // Session s1 =sessionFactory1.openSession(); 
       // Transaction tx1= s1.beginTransaction(); 
       Transaction tx = s.beginTransaction(); 
       Student stu = new Student(); 
       stu.setName(name); 
       stu.setDegree(degree); 
       stu.setPhone(phone); 
       s.save(stu); 
       tx.commit(); 
       System.out.println("Added to oracle Database"); 
       if (s != null) 
        s.close(); 

       // Student1 stu1=new Student1(); 
       // stu1.setName(name1); 
       // s1.save(stu1); 
       // tx1.commit(); 
       // System.out.println("Added to mysql Database"); 
       // if (s1 != null) 
       // s1.close(); 
      } 
     // }// end of if A 
    }// end of method 
}// end of class 

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory name="studentFactory"> 
     <property name="connection.driver_class"> 
      oracle.jdbc.OracleDriver 
      </property> 
     <property name="connection.url"> 
      jdbc:oracle:thin:@localhost:1521:test 
     </property> 
     <property name="connection.username"> 
      system 
     </property> 
     <property name="connection.password"> 
      manager 
     </property> 
     <property name="connection.pool_size">5</property> 
     <!-- SQL dialect --> 
     <property name="dialect"> 
      org.hibernate.dialect.OracleDialect 
      </property> 
     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 
     <property name="hbm2ddl.auto">update</property> 
     <mapping resource="Student.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 

Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="hibernatepractise.Student" table="studentOracle1"> 
     <id name="id" type="long" column="ID"> 
      <generator class="increment" /> 
     </id> 
     <property name="name" column="name" not-null="true" /> 
     <property name="degree" column="degree" /> 
     <property name="phone" column="phone" /> 
    </class> 
</hibernate-mapping> 

Je suis confronté à des erreurs suivantes dans mon code

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection 
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) 
    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 hibernatepractise.AddStudent.main(AddStudent.java:42) 
Caused by: java.sql.SQLException: Listener refused the connection with the following error: 
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor 

    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70) 
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133) 
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199) 
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480) 
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413) 
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508) 
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203) 
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33) 
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510) 
    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: oracle.net.ns.NetException: Listener refused the connection with the following error: 
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor 

    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:361) 
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966) 
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292) 
    ... 13 more 

Je sais qu'il ya une erreur stupide dans mon code, mais je ne suis pas en mesure de Découvrez comme je suis nouveau à hiberner, c'est pourquoi je vous demande de m'aider s'il vous plaît à sortir de ce problème. Je serai très reconnaissant envers vous pour cela.

Merci à l'avance.

+2

quelle erreur que vous obtenez? avez-vous une trace de pile à partager? –

+1

hey j'ajoute les erreurs dans les questions ci-dessus. –

+0

oui et: https://stackoverflow.com/questions/39605087/exception-in-thread-main-org-hibernate-exception-genericjdbcexception-cannot –

Répondre

0

Je crois que l'erreur se trouve dans cette ligne:

jdbc:oracle:thin:@localhost:1521:test. Je pense qu'il devrait être jdbc:oracle:thin:@localhost:1521/test

A / au lieu d'un :

+0

Je l'ai déjà essayé, mais ça ne m'a pas aidé. :-(@Jack Flamp –

+0

@JayeshVyas voir mise à jour –

+0

J'ai essayé cela aussi, mais cela n'a pas aidé.hey si j'utilise le port 8088 alors ??? –