Coud不会向数据库发送题字表

时间:2019-07-06 10:53:02

标签: spring hibernate

我想将其发送到h2database时为客户创建题字表单 它会创建联合国错误消息

它的工作原理是在jsp页面中没有发货明细和账单明细,而在customerdaoimpl中,我修改了诸如session.saveorUpdate(customer)之类的代码

org.hibernate.StaleStateException:批处理更新从更新[0]返回意外行数;实际行数:0;预期:1

我正在处理Intellij,h2数据库,tomcat9服务程序

我正在与一位反应迟钝的老师一起在互联网上浏览一个tuto,但是他的代码正在唤醒

enter code here注册课程

@Controller
public class RegisterController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/register")
    public String registerCustomer(Model model){
        Customer customer = new Customer();
        BillingAddress billingAddress = new BillingAddress();
        ShippingAddress shippingAddress = new ShippingAddress();
        customer.setBillingAddress(billingAddress);
        customer.setShippingAddress(shippingAddress);

        model.addAttribute("customer", customer);

        return "registerCustomerFormPage";

    }

    @RequestMapping(value= "/register" , method= RequestMethod.POST)
    public String registerCustomerPost(@ModelAttribute("customer") Customer customer, Model model){

        customer.setEnabled(true);

        customerService.addCustomer(customer);

        return "registerCustomerSuccessPage";

    }
}

    enter code here

customerDaoIMPL class

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Repository
@Transactional
public class CustomerDaoImpl implements CustomerDao {

    @Autowired
    private SessionFactory sessionFactory;

    public void addCustomer(Customer customer){

        Session session= sessionFactory.getCurrentSession();

        customer.getBillingAddress().setCustomer(customer);

        customer.getShippingAddress().setCustomer(customer);
        session.saveOrUpdate(customer);
        session.saveOrUpdate(customer.getBillingAddress());
        session.saveOrUpdate(customer.getShippingAddress());

        Users newUser = new Users();

        newUser.setUsername(customer.getUsername());
        newUser.setPassword(customer.getPassword());
        newUser.setEnabled(true);
        newUser.setUserId(customer.getCustomerId());

        Authorities newAuthority = new Authorities();
        newAuthority.setUsername(customer.getUsername());
        newAuthority.setAuthority("ROLE_USER");

        session.saveOrUpdate(newUser);
        session.saveOrUpdate(newAuthority);

        Cart newCart = new Cart();
        newCart.setCustomer(customer);
        customer.setCart(newCart);
        session.saveOrUpdate(customer);
        session.saveOrUpdate(newCart);

        session.flush();

    }
}

customer class(i have getter and setter in my code)


@Entity
public class Customer implements Serializable {


    private static final long serialVersionUID = 8555048716003099908L;

    @Id
    @GeneratedValue
    private int customerId;

    @NotEmpty(message = "Veuillez saisir votre nom.")
    private String customerLastname;

    @NotEmpty(message = "Veuillez saisir votre prenom.")
    private String customerName;

    @NotEmpty(message="Veuillez saisir votre email")
    private String customerEmail;

    private String customerPhone;

    @NotEmpty(message = "Veuillez saisir votre identifiant.")
    private String username;

    @NotEmpty(message = "Veuillez saisir votre mot de passe.")
    private String password;


    private boolean enabled;

    @OneToOne
    @JoinColumn(name = "billingAddressId")
    private BillingAddress billingAddress;

    @OneToOne
    @JoinColumn(name = "shippingAddressId")
    private ShippingAddress shippingAddress;


    @OneToOne
    @JoinColumn(name="cartId")
    @JsonIgnore
    private Cart cart;

    @OneToOne
    @JoinColumn(name="customerOrderId")
    @JsonIgnore
    private CustomerOrder customerOrder;


shipping class (i have getter and setter in my code)


@Entity
public class ShippingAddress implements Serializable {


    private static final long serialVersionUID = -980949691137452051L;


    @Id
    @GeneratedValue
    private  int shippingAddressId;

    private String apartmentNumber;
    private String streetName;

    private String postalCode;
    private String city;
    private String country;

    @OneToOne
    private  Customer customer;

    enter code here

<div class="container-wrapper">
    <div class="container">
        <div class="page-header">
            <h1>Products Details</h1>
            <p class="lead">Inscription </p>
        </div>

        <form:form action="${pageContext.request.contextPath}/register" method="post" commandName="customer" >

            <div class="form-group">
                <label for="lastname">Nom</label>
                <form:input path="customerLastname" id="lastname" class="form-Control"/>
            </div>

            <div class="form-group">
                <label for="name">Prenom</label>
                <form:input path="customerName" id="name" class="form-Control"/>
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <form:input path="customerEmail" id="email" class="form-Control"/>
            </div>
            <div class="form-group">
                <label for="phone">Telephone</label>
                <form:input path="customerPhone" id="phone" class="form-Control"/>
            </div>
            <div class="form-group">
                <label for="username">Identifiant</label>
                <form:input path="username" id="username" class="form-Control"/>
            </div>
            <div class="form-group">
                <label for="password">Mot de Passe</label>
                <form:password path="password" id="password" class="form-Control"/>
            </div>


           <h3>Adresse de facturation</h3>



            <div class="form-group">
                <label for="billingApartmentNumber">Numero </label>
                <form:input path="billingAddress.apartmentNumber" id="billingApartmentNumber" class="form-Control"/>
            </div>


            <div class="form-group">
                <label for="billingStreet">Rue</label>
                <form:input path="billingAddress.streetName" id="billingStreet" class="form-Control"/>
            </div>


            <div class="form-group">
                <label for="billingPostalCode">Code Postale</label>
                <form:input path="billingAddress.postalCode" id="billingPostalCode" class="form-Control"/>
            </div>


            <div class="form-group">
                <label for="billingCity">Ville</label>
                <form:input path="billingAddress.city" id="billingCity" class="form-Control"/>
            </div>

            <div class="form-group">
                <label for="billingCountry">Pays</label>
                <form:input path="billingAddress.country" id="billingCountry" class="form-Control"/>
            </div>



            <h3>Adresse de livraison</h3>




            <div class="form-group">
                <label for="shippingApartmentNumber">Numero</label>
                <form:input path="shippingAddress.apartmentNumber" id="shippingApartmentNumber" class="form-Control"/>
            </div>


            <div class="form-group">
                <label for="shippingStreet">Rue</label>
                <form:input path="shippingAddress.streetName" id="shippingStreet" class="form-Control"/>
            </div>



            <div class="form-group">
                <label for="shippingPostalCode">Code Postale</label>
                <form:input path="shippingAddress.postalCode" id="shippingPostalCode" class="form-Control"/>
            </div>


            <div class="form-group">
                <label for="shippingCity">Ville</label>
                <form:input path="shippingAddress.city" id="shippingCity" class="form-Control"/>
            </div>

            <div class="form-group">
                <label for="shippingCountry">Pays</label>
                <form:input path="shippingAddress.country" id="shippingCountry" class="form-Control"/>
            </div>






            <br></br>

            <input type="submit" value="submit" class="btn btn-default">
            <a href="<c:url value="/"/> btn btn-cancel">Cancel</a>


        </form:form>



billing class((i have getter and setter in my code)

@Entity
public class BillingAddress implements Serializable {


    private static final long serialVersionUID = -4057446516133133489L;

    @Id
    @GeneratedValue
    private  int billingAddressId;

    private String apartmentNumber;
    private String streetName;

    private String postalCode;
    private String city;
    private String country;

    @OneToOne
    private  Customer customer;

application ontext

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security.xsd">





    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:tcp://localhost/~/test" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.intelj</value>
            </list>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1024000" />
    </bean>

    <!--=======================http configuration==================================-->


    <security:http auto-config="true">
        <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN"  />
        <security:intercept-url pattern="/customer/**" access="ROLE_USER"  />
        <security:form-login
                login-page="/login"
                login-processing-url="/j_spring_security_check"
                default-target-url="/product/viewProductList/"
                authentication-failure-url="/login?error"

                username-parameter="username"
                password-parameter="password" />

        <security:logout logout-success-url="/login?logout" />
    </security:http>


    <!--=============================================================================-->


    <!-- ====================Authentifacation manager================================-->


    <security:authentication-manager>
        <security:authentication-provider>
            <security:jdbc-user-service data-source-ref="dataSource"
                                        authorities-by-username-query="SELECT username, authority From authorities WHERE username = ?"

                                        users-by-username-query="SELECT username, password, enabled FROM users WHERE username = ?" />
        </security:authentication-provider>
    </security:authentication-manager>


    <!--=============================================================================-->

错误消息

例外情况

消息请求处理失败;嵌套的异常是org.hibernate.StaleStateException:批处理更新从更新[0]返回了意外的行数;实际行数:0;预期:1

说明需要提供任职资格的人。

例外

org.springframework.web.util.NestedServletException:请求处理失败;嵌套的异常是org.hibernate.StaleStateException:批处理更新从更新[0]返回了意外的行数;实际行数:0;预期:1     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)     org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)     javax.servlet.http.HttpServlet.service(HttpServlet.java:660)     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     javax.servlet.http.HttpServlet.service(HttpServlet.java:741)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)     org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)     org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)     org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)     org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)     org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347)     org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263) 起因

org.hibernate.StaleStateException:批处理更新从更新[0]返回意外行数;实际行数:0;预期:1     org.hibernate.jdbc.Expectations $ BasicExpectation.checkBatched(Expectations.java:81)     org.hibernate.jdbc.Expectations $ BasicExpectation.verifyOutcome(Expectations.java:73)     org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:57)     org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3006)     org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2908)     org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3237)     org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:113)     org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)     org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:264)     org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:187)     org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)     org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)     org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1081)     com.intelj.dao.impl.CustomerDaoImpl.addCustomer(CustomerDaoImpl.java:56)     sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     java.lang.reflect.Method.invoke(Method.java:498)     org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)     org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)     org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)     org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)

0 个答案:

没有答案