hibernate和Spring错误:创建表但不插入

时间:2016-10-10 19:24:00

标签: spring hibernate

你好w enter image description here我执行它显示的代码:

INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1622fdf: defining beans [daoClient,serviceClient,daoProduit,serviceProduit,dataSource,persistenceUnitManager,entityMangerFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor]; root of factory hierarchy
Hibernate: alter table PRODUITS drop foreign key FKF2D1D7EAA6CDC455
Hibernate: drop table if exists Client
Hibernate: drop table if exists PRODUITS
Hibernate: create table Client (idClient bigint not null auto_increment, adressClient varchar(255), nomClient varchar(255), prenomClient varchar(255), telephoneClient varchar(255), primary key (idClient))
Hibernate: create table PRODUITS (idProduit bigint not null auto_increment, descriptionProduit varchar(255), designationProduit varchar(255), prixProduit double precision not null, qteProduit integer not null, idClient bigint, primary key (idProduit))
Hibernate: alter table PRODUITS add index FKF2D1D7EAA6CDC455 (idClient), add constraint FKF2D1D7EAA6CDC455 foreign key (idClient) references Client (idClient)
Exception in thread "main" java.lang.NullPointerException
    at com.axiz.projet.dao.IClientImp.addClient2(IClientImp.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy3.addClient2(Unknown Source)
    at com.axiz.projet.services.ClientServiceImp.addClient2(ClientServiceImp.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy4.addClient2(Unknown Source)
    at com.axiz.projet.Test.main(Test.java:23)

我的测试类是: testClass

的ApplicationContext

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    <bean id="daoClient" class="com.axiz.projet.dao.IClientImp"/>
    <bean id="serviceClient" class="com.axiz.projet.services.ClientServiceImp">
        <property name="daoClient" ref="daoClient"></property>
    </bean>
    <bean id="daoProduit" class="com.axiz.projet.dao.IProduitImpl"/>
    <bean id="serviceProduit" class="com.axiz.projet.services.ProduitServiceImp">
        <property name="daoProduit" ref="daoProduit"></property>
    </bean>
    <!--  -->
    <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/bd_produit_hibernate" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>
    <!--  -->
    <bean id="persistenceUnitManager" depends-on="transactionManager"
      class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"/>
    <property name="defaultDataSource" ref="dataSource"/>
    <property name="dataSourceLookup">
        <bean class="org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup"/>
    </property>
</bean>
    <!-- ******************** -->


    <bean id="entityMangerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

       <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
        <property name="persistenceUnitName" value="prodClientPersistence"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityMangerFactory"/>
    </bean>


    <tx:annotation-driven transaction-manager="transactionManager" />




</beans>

porm.xml 强文

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.axiz</groupId>
    <artifactId>projet</artifactId>
    <name>prodCatProjet</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.6</java-version>
        <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>   

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.9.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.21</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

IClientServiceImplementation类 包com.axiz.projet.services;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.axiz.projet.dao.IClient;
import com.axiz.projet.entities.Client;
@Transactional
public class ClientServiceImp implements IClientService {
    private IClient daoClient;
    @Override
    public Client addClient(Client client) {

        return daoClient.addClient(client);
    }

    @Override
    public List<Client> getAll() {
        // TODO Auto-generated method stub
        return daoClient.getAll();
    }

    @Override
    public Client getClientById(Long idClient) {
        // TODO Auto-generated method stub
        return daoClient.getClientById(idClient);
    }

    @Override
    public void deleteClient(Long idClient) {
        daoClient.deleteClient(idClient);

    }

    @Override
    public Client updateClient(Client client) {
        // TODO Auto-generated method stub
        return daoClient.updateClient(client);
    }

    public void setDaoClient(IClient daoClient) {
        this.daoClient = daoClient;
    }

    @Override
    public void addClient2(Client client) {
        daoClient.addClient2(client);

    }

}

IClientDao

package com.axiz.projet.dao;

import java.util.List;
import java.util.concurrent.Executor;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import com.axiz.projet.entities.Client;
@Transactional
public class IClientImp implements IClient {
    @PersistenceContext
    private EntityManager em;
    @Override
    public Client addClient(Client client) {

        em.persist(client);


        return client;
    }

    @Override
    public List<Client> getAll() {
        Query query=em.createQuery("select c from Client c");
        return query.getResultList();
    }

    @Override
    public Client getClientById(Long idClient) {
        Client client=em.find(Client.class, idClient);
        return client;
    }

    @Override
    public void deleteClient(Long idClient) {
        em.remove(getClientById(idClient));

    }

    @Override
    public Client updateClient(Client client) {
        // TODO Auto-generated method stub
        return em.merge(client);
    }


    public void addClient2(Client client) {
        // TODO Auto-generated method stub
        em.persist(client);
    }

}

0 个答案:

没有答案