数据库连接错误

时间:2015-04-26 15:32:55

标签: java mysql hibernate

我一直致力于一个项目,这是一个简单的Spring,Hibernate,JSF,MySQL集成;我在Eclipse上运行它。该程序的目的是在MySQLWorkbench上添加Person(id,名字,姓氏,性别,年龄等)数据库并在服务器上运行它。我使用Spring4,Hibernate4,Primefaces 5.0.2和Eclipse Luna作为工具。我将项目构建为maven。 当我在main上运行程序时,我想看到我的表中包含一个新的Person。 我在终端上为我创建的用户使用了“GRANT ALL ON”和“FLUSH PRIVILEGES”命令,并收到“Query OK”消息。 尽管在必要的文件上指定了用户名和密码,但我无法在我的表中添加新记录。我的项目结构如下:

enter image description here

其他文件是:

domain-classes.hbm.xml

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class table="testPerson" name="com.hibernate.data.Person">
        <id column="person_id" type="int" name="id">
            <generator class="increment" />
        </id>
        <property name="firstName" column="person_firstname" type="string" />
        <property name="lastName" column="person_lastname" type="string" />
        <property name="gender" column="person_gender" type="string" />
        <property name="age" column="person_age" type="int" />
    </class>
</hibernate-mapping>

hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 <hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/PERSONDB</property>
        <property name="hibernate.connection.username">fulden2</property>
        <property name="hibernate.connection.password">secret_pass</property>
        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Show SQL -->
        <property name="show_sql">true</property>
        <!-- Specify session context -->
        <property name="hibernate.current_session_context_class">thread</property>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
        <!-- Autocommit -->
        <property name="hibernate.connection.autocommit">false</property> 
        <!-- Referring Mapping File -->
        <mapping resource="domain-classes.hbm.xml" />
    </session-factory>
 </hibernate-configuration>

applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <!-- Enable Spring Annotation Configuration -->
    <context:annotation-config />
    <!-- Scan for all of Spring components such as Spring Service -->
    <context:component-scan base-package="com.*"></context:component-scan>
    <!-- Create Data Source bean -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/PERSONDB" />
        <property name="username" value="fulden2" />
        <property name="password" value="secret_pass" />
    </bean>
    <!-- Define SessionFactory bean -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>
    <!-- Transaction Manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- Detect @Transactional Annotation -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

的pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>PersonApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>PersonApp</name>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.4</source>
          <target>1.4</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <url>http://maven.apache.org</url>
  <repositories>
     <repository>
         <id>prime-repo</id>
         <name>PrimeFaces Maven Repository</name>
         <url>http://repository.primefaces.org</url>
         <layout>default</layout>
     </repository>
   </repositories>
 <dependencies>
  <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
   <!-- Faces Implementation -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.10</version>
    </dependency>
    <!-- Faces Library -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.10</version>
    </dependency>
    <!-- Primefaces Version 5 -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.2</version>
    </dependency>
    <!-- JSP Library -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
    <!-- JSTL Library -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- Primefaces Theme Library -->
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>blitzer</artifactId>
        <version>1.0.10</version>
    </dependency>
    <!-- Hibernate library -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.9.Final</version>
    </dependency>
    <!-- MySQL driver connector library -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
    </dependency>
    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <!-- Spring Web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
 </dependencies>
</project>

主要课程:

    package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.hibernate.data.Person;

public class Main {
    public static void main(String [] args){
        Configuration configuration = new Configuration();
        System.out.println("CFG and hbm files loaded successfully.");//just to test

        configuration.configure("hibernate.cfg.xml");

        SessionFactory factory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().configure().build());

        Session session = factory.openSession();

        Transaction tx = session.getTransaction();
        tx.begin();
        System.out.println("Transaction began");//just to test

        Person newPerson = new Person();
        newPerson.setFirstName("aa");
        newPerson.setLastName("bbb");
        newPerson.setGender("Male");
        newPerson.setAge(2);

        session.save(newPerson);
        session.flush();

        tx.commit();
        session.close();
        System.out.println("Session closed");//just to test
    }

}

是否有额外的步骤来实现此数据库连接?

或者我的错误是什么?

感谢您的帮助..

1 个答案:

答案 0 :(得分:0)

可能你的会话没有被刷新;可能是您配置中的一个小故障。如果您已配置OpenSessionInView过滤器它会更改配置的默认刷新模式,则会为实例多次执行此操作。在DAO层中检查flushMode session.getFlushMode() 刷新模式不应为NEVERMANUAL

如果没有刷新会话,则不会更新db。您必须在事务后session.flush()

手动刷新会话

同样在in配置文件中,只需将属性从false&amp;更改为true即可。检查<property name="connection.autocommit">true</property>