Hibernate配置中的异常

时间:2017-10-20 07:10:51

标签: java database spring hibernate jdbc

我是Spring,Hibernate的新手。在hibernate上工作时我遇到了以下问题。我还在stackoverflow上搜索了相关标签,但找不到任何解决我问题的相关帖子。

Student.java文件

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文件

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>

我的代码中遇到以下错误

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

我知道我的代码中有一个愚蠢的错误,但我无法找到因为我是新手来休眠所以这就是为什么我请求你帮助我摆脱这个问题。 我将非常感谢你。

先谢谢。

1 个答案:

答案 0 :(得分:0)

我认为错误在于这一行:

jdbc:oracle:thin:@localhost:1521:test。我认为应该是 jdbc:oracle:thin:@localhost:1521/test

/代替: