【问题标题】:GAE + JPA + Spring - not able to insert data into database (Mysql)GAE + JPA + Spring - 无法将数据插入数据库(Mysql)
【发布时间】:2014-07-23 12:06:03
【问题描述】:

朋友们好,如果有人能帮助我解决这个问题,那就太好了。 我正在尝试在谷歌应用引擎上构建一个网络应用程序。我无法使用 jpa 将数据写入数据库,也不例外。我尝试使用flush bt,它给了我如下异常:

javax.persistence.TransactionRequiredException: no transaction is in progress
  at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:301)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
  at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
  at com.sun.proxy.$Proxy31.flush(Unknown Source)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
  at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
  at com.sun.proxy.$Proxy31.flush(Unknown Source)
  at com.bullbeardevice.dao.impl.BaseDAOImpl.save(BaseDAOImpl.java:52)
  at com.bullbeardevice.service.impl.LoginServiceImpl.authenthicateUserLogin(LoginServiceImpl.java:70)
  at com.bullbeardevice.controller.common.LoginController.authenticateUserLogin(LoginController.java:35)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
  at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
  at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
  at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
  at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:127)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
  at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
  at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
  at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
  at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
  at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
  at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
  at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
  at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
  at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
  at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
  at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
  at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
  at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:490)
  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
  at org.mortbay.jetty.Server.handle(Server.java:326)
  at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
  at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
  at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
  at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
  at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
  at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
  at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
2014-07-23 17:27:39,731 [1509718305@qtp-1651034737-0] DEBUG (LoginController.java:46) – End of method loginUser of class LoginController

如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

  • 这并没有提供太多的东西。您能否分享更多细节,例如来自LoginServiceImpl.authenthicateUserLoginBaseDAOImpl.save 的sn-ps(包括任何注释)、Spring JPA 配置等?
  • 嗨,Manish,感谢您的回复,我的问题已解决,但我在获取记录时遇到了另一个问题 org.hibernate.exception.SQLGrammarException:“字段列表”中的未知列“userlogind0_.jdoDetachedState”。我在查询和我的表或实体中没有任何这样的字段。我相信这是用来跟踪对象的状态 bt 请告诉我如何处理这个。

标签: java google-app-engine spring-mvc jpa-2.0 spring-transactions


【解决方案1】:

Google App Engine uses the DataNucleus Access Platform 作为 JPA 1.0 实现。默认情况下,DataNucleus JPA 提供程序希望每个注释为 @Entity 的类都有一个 jdoDetachedState 属性,映射到数据库表中的适当列。

您有以下选择:

  1. 向实体添加一个名为 jdoDetachedState 的属性,并向表中添加适当的列。
  2. 按照in this Google Forums post 所述的步骤,使用 DataNucleus 关闭分离模式。
  3. 为每个实体添加一个名为 jdoDetachedStatetransient 属性,如 in this DataNucleus JIRA 所述。

【讨论】:

  • 如果我必须使用第一个选项,那么 DB 中 jdoDetachedState 的数据类型是什么
  • 你真的要work with objects in the detached mode吗?如果您有一个干净的架构,其中只有业务逻辑加载模型对象并对其进行处理,则 DataNucleus 不需要可拆卸对象,您应该使用上面列出的选项 #2。如果您决定使用选项#1,Java 数据类型将为Object[],数据库类型应为BLOB。但同样,此属性仅在运行时由 DataNucleus 使用,并且永远不会持久化,因此您应该尽量避免使用选项 #1。
  • 感谢 Manish 的快速回复。我已经使用了选项 2 bt,该解决方案的问题是它随机工作。有时它可以工作,有时它会给出相同的错误。
  • 还有一个问题我想问你,因为我是应用程序引擎的新手,这是由于我的实体中有多个@onetomany 映射造成的。?
  • 关闭分离模式应该一直有效。只需检查您是否正确注释了所有实体。首先在 GAE 上尝试一个示例项目也很有用,只有一个实体。使该工作几次没有错误。然后添加另一个实体并使其工作。再重复一次,以证明一切正常。请记住,GAE 提供了受限制的 JEE 环境,因此在第一次尝试时可能无法正常工作,您可能必须找到解决方法。至于您的其他问题,请将其作为新问题发布以供他人检查。
【解决方案2】:

我的实体类如下所示:

package com.bullbeardevice.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * The persistent class for the user_registration database table.
 * 
 */
@Entity
@Table(name = "user_registration")
@NamedQuery(name = "UserRegistration.findAll", query = "SELECT u FROM UserRegistration u")
public class UserRegistration implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_Registration_Id")
    private String userRegistrationId;

    @Column(name = "account_Status")
    private int accountStatus;

    @Column(name = "created_By")
    private String createdBy;

    @Column(name = "created_On")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdOn;

    @Column(name = "email_Address")
    private String emailAddress;

    @Column(name = "expiry_Date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date expiryDate;

    @ManyToOne
    @JoinColumn(name = "Group_Master_Id")
    private GroupMaster groupMaster;

    @Column(name = "modified_By")
    private String modifiedBy;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "modified_On")
    private Date modifiedOn;

    @Column(name = "user_password")
    private String password;

    @Column(name = "permissions")
    private int permissions;

    // bi-directional many-to-one association to Chat
    @OneToMany(mappedBy = "userRegistration")
    private List<Chat> chats;

    // bi-directional many-to-one association to Coupon
    @OneToMany(mappedBy = "userRegistration")
    private List<Coupon> coupons;

    // bi-directional many-to-one association to DeviceDetail
    @OneToMany(mappedBy = "userRegistration")
    private List<DeviceDetail> deviceDetails;

    // bi-directional many-to-one association to Portfolio
    @OneToMany(mappedBy = "userRegistration")
    private List<Portfolio> portfolios;

    // bi-directional many-to-one association to UserDetail
    @OneToMany(mappedBy = "userRegistration")
    private List<UserDetail> userDetails;

    // bi-directional many-to-one association to UserLoginDetail
    @OneToMany(mappedBy = "userRegistration")
    private List<UserLoginDetail> userLoginDetails;

    // bi-directional many-to-one association to UserRoleMaster
    @ManyToOne
    @JoinColumn(name = "User_Role_Master_Id")
    private UserRoleMaster userRoleMaster;

    // bi-directional many-to-one association to Watch
    @OneToMany(mappedBy = "userRegistration")
    private List<Watch> watches;

    public UserRegistration() {
    }

    /**
     * @return the userRegistrationId
     */
    public String getUserRegistrationId() {
        return userRegistrationId;
    }

    /**
     * @param userRegistrationId
     *            the userRegistrationId to set
     */
    public void setUserRegistrationId(String userRegistrationId) {
        this.userRegistrationId = userRegistrationId;
    }

    /**
     * @return the accountStatus
     */
    public int getAccountStatus() {
        return accountStatus;
    }

    /**
     * @param accountStatus
     *            the accountStatus to set
     */
    public void setAccountStatus(int accountStatus) {
        this.accountStatus = accountStatus;
    }

    /**
     * @return the createdBy
     */
    public String getCreatedBy() {
        return createdBy;
    }

    /**
     * @param createdBy
     *            the createdBy to set
     */
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * @return the createdOn
     */
    public Date getCreatedOn() {
        return createdOn;
    }

    /**
     * @param createdOn
     *            the createdOn to set
     */
    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    /**
     * @return the emailAddress
     */
    public String getEmailAddress() {
        return emailAddress;
    }

    /**
     * @param emailAddress
     *            the emailAddress to set
     */
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    /**
     * @return the expiryDate
     */
    public Date getExpiryDate() {
        return expiryDate;
    }

    /**
     * @param expiryDate
     *            the expiryDate to set
     */
    public void setExpiryDate(Date expiryDate) {
        this.expiryDate = expiryDate;
    }

    /**
     * @return the groupMaster
     */
    public GroupMaster getGroupMaster() {
        return groupMaster;
    }

    /**
     * @param groupMaster
     *            the groupMaster to set
     */
    public void setGroupMaster(GroupMaster groupMaster) {
        this.groupMaster = groupMaster;
    }

    /**
     * @return the modifiedBy
     */
    public String getModifiedBy() {
        return modifiedBy;
    }

    /**
     * @param modifiedBy
     *            the modifiedBy to set
     */
    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    /**
     * @return the modifiedOn
     */
    public Date getModifiedOn() {
        return modifiedOn;
    }

    /**
     * @param modifiedOn
     *            the modifiedOn to set
     */
    public void setModifiedOn(Date modifiedOn) {
        this.modifiedOn = modifiedOn;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     *            the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the permissions
     */
    public int getPermissions() {
        return permissions;
    }

    /**
     * @param permissions
     *            the permissions to set
     */
    public void setPermissions(int permissions) {
        this.permissions = permissions;
    }

    /**
     * @return the chats
     */
    public List<Chat> getChats() {
        return chats;
    }

    /**
     * @param chats
     *            the chats to set
     */
    public void setChats(List<Chat> chats) {
        this.chats = chats;
    }

    public Chat addChat(Chat chat) {
        getChats().add(chat);
        chat.setUserRegistration(this);

        return chat;
    }

    public Chat removeChat(Chat chat) {
        getChats().remove(chat);
        chat.setUserRegistration(null);

        return chat;
    }

    public List<Coupon> getCoupons() {
        return this.coupons;
    }

    public void setCoupons(List<Coupon> coupons) {
        this.coupons = coupons;
    }

    public Coupon addCoupon(Coupon coupon) {
        getCoupons().add(coupon);
        coupon.setUserRegistration(this);

        return coupon;
    }

    public Coupon removeCoupon(Coupon coupon) {
        getCoupons().remove(coupon);
        coupon.setUserRegistration(null);

        return coupon;
    }

    public List<DeviceDetail> getDeviceDetails() {
        return this.deviceDetails;
    }

    public void setDeviceDetails(List<DeviceDetail> deviceDetails) {
        this.deviceDetails = deviceDetails;
    }

    public DeviceDetail addDeviceDetail(DeviceDetail deviceDetail) {
        getDeviceDetails().add(deviceDetail);
        deviceDetail.setUserRegistration(this);

        return deviceDetail;
    }

    public DeviceDetail removeDeviceDetail(DeviceDetail deviceDetail) {
        getDeviceDetails().remove(deviceDetail);
        deviceDetail.setUserRegistration(null);

        return deviceDetail;
    }

    public List<Portfolio> getPortfolios() {
        return this.portfolios;
    }

    public void setPortfolios(List<Portfolio> portfolios) {
        this.portfolios = portfolios;
    }

    public Portfolio addPortfolio(Portfolio portfolio) {
        getPortfolios().add(portfolio);
        portfolio.setUserRegistration(this);

        return portfolio;
    }

    public Portfolio removePortfolio(Portfolio portfolio) {
        getPortfolios().remove(portfolio);
        portfolio.setUserRegistration(null);

        return portfolio;
    }

    public List<UserDetail> getUserDetails() {
        return this.userDetails;
    }

    public void setUserDetails(List<UserDetail> userDetails) {
        this.userDetails = userDetails;
    }

    public UserDetail addUserDetail(UserDetail userDetail) {
        getUserDetails().add(userDetail);
        userDetail.setUserRegistration(this);

        return userDetail;
    }

    public UserDetail removeUserDetail(UserDetail userDetail) {
        getUserDetails().remove(userDetail);
        userDetail.setUserRegistration(null);

        return userDetail;
    }

    public List<UserLoginDetail> getUserLoginDetails() {
        return this.userLoginDetails;
    }

    public void setUserLoginDetails(List<UserLoginDetail> userLoginDetails) {
        this.userLoginDetails = userLoginDetails;
    }

    public UserLoginDetail addUserLoginDetail(UserLoginDetail userLoginDetail) {
        getUserLoginDetails().add(userLoginDetail);
        userLoginDetail.setUserRegistration(this);

        return userLoginDetail;
    }

    public UserLoginDetail removeUserLoginDetail(UserLoginDetail userLoginDetail) {
        getUserLoginDetails().remove(userLoginDetail);
        userLoginDetail.setUserRegistration(null);

        return userLoginDetail;
    }

    public UserRoleMaster getUserRoleMaster() {
        return this.userRoleMaster;
    }

    public void setUserRoleMaster(UserRoleMaster userRoleMaster) {
        this.userRoleMaster = userRoleMaster;
    }

    public List<Watch> getWatches() {
        return this.watches;
    }

    public void setWatches(List<Watch> watches) {
        this.watches = watches;
    }

    public Watch addWatch(Watch watch) {
        getWatches().add(watch);
        watch.setUserRegistration(this);

        return watch;
    }

    public Watch removeWatch(Watch watch) {
        getWatches().remove(watch);
        watch.setUserRegistration(null);

        return watch;
    }

}

【讨论】:

    猜你喜欢
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2013-04-28
    相关资源
    最近更新 更多