0

我如何将数据插入到多个表(MySQL)中Java EE&JBoss7.1. 我做了几个简单的教程,但现在我有一个表和一个文件,我在其中添加了我的数据,并且从 EntityManager 调用了添加数据的这个文件方法。现在我想使用许多具有关系的表。我添加了一个我想做的简单示例。

结构表

我的表(类)是使用 JPA 2.0 生成的。

用户.java:

@Entity
@Table(name="users")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String name;

    private String password;

    //bi-directional one-to-one association to Profile
    @OneToOne(mappedBy="user")
    private Profile profile;

    //bi-directional many-to-one association to UserRole
    @OneToMany(mappedBy="user")
    private Set<UserRole> userRoles;

    //bi-directional many-to-many association to Activity
    @ManyToMany
    @JoinTable(
        name="user_activity"
        , joinColumns={
            @JoinColumn(name="users_id")
            }
        , inverseJoinColumns={
            @JoinColumn(name="activities_id")
            }
        )
    private Set<Activity> activities;

    public User() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Profile getProfile() {
        return this.profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    public Set<UserRole> getUserRoles() {
        return this.userRoles;
    }

    public void setUserRoles(Set<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

    public Set<Activity> getActivities() {
        return this.activities;
    }

    public void setActivities(Set<Activity> activities) {
        this.activities = activities;
    }

}

用户角色.java:

@Entity
@Table(name="user_roles")
public class UserRole implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private UserRolePK id;

    private String name;

    //bi-directional many-to-one association to User
    @ManyToOne
    @JoinColumn(name="users_id", insertable = false, updatable = false)
    private User user;

    public UserRole() {
    }

    public UserRolePK getId() {
        return this.id;
    }

    public void setId(UserRolePK id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

用户角色PK.java:

@Embeddable
public class UserRolePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="users_id")
    private int usersId;

    @Column(name="role_name")
    private String roleName;

    public UserRolePK() {
    }
    public int getUsersId() {
        return this.usersId;
    }
    public void setUsersId(int usersId) {
        this.usersId = usersId;
    }
    public String getRoleName() {
        return this.roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof UserRolePK)) {
            return false;
        }
        UserRolePK castOther = (UserRolePK)other;
        return 
            (this.usersId == castOther.usersId)
            && this.roleName.equals(castOther.roleName);

    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.usersId;
        hash = hash * prime + this.roleName.hashCode();

        return hash;
    }
}

数据已插入“用户”,但其余部分不起作用。

我写了这个:

@ManagedBean(name="bean")
//@SessionScoped
//@LocalBean

public class RegistrationBean implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 3158649307217736396L;
    //////////////////////////////////////////////////////////////////


        public String name;
        public String password;
        public int id;

        public int usersId = id;
        public String roleName="user";

        public Set<UserRole> userRoles;


        @EJB
        private UserService us;

        public int getUsersId() {
            return usersId;
        }

        public void setUsersId(int usersId) {
            this.usersId = usersId;
        }

        public String getRoleName() {
            return roleName;
        }

        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String addUser() {
            User u = new User();
            UserRolePK urpk = new UserRolePK();
            UserRole ur = new UserRole();
            u.setName(name);
            u.setPassword(password);
            u.setId(id);

            urpk.setUsersId(usersId);
            urpk.setRoleName(roleName);

            ur.setName(name);
            ur.setUser(u);

            us.addUser(u);
            us.addUser(urpk);
            us.addUser(ur);
            return "registerbean";

        }
}

和:

@Stateless
@LocalBean
//@ManagedBean
public  class UserService implements UserServiceInterface {



    @PersistenceContext(unitName="virtual.trainer.library")
    private EntityManager em;


    public UserService() {

    }


    @SuppressWarnings("unchecked")
    @Override
    public List<User> getUsers() {
        return em.createQuery("FROM User").getResultList();
    }

    public void addUser(User u) {
        // TODO Auto-generated method stub
        em.persist(u);
    }


    public void addUser(UserRolePK urpk) {
        // TODO Auto-generated method stub
        em.persist(urpk);
    }


    public void addPK(UserRolePK urpk) {
        // TODO Auto-generated method stub
        em.persist(urpk);
    }


    public void addUser(UserRole ur) {
        // TODO Auto-generated method stub
        em.persist(ur);
    }


}

好的,我制作了“UserTransaction”JTA,但我仍然有问题:

@ManagedBean(name="bean")
//@SessionScoped
//@LocalBean

public class RegistrationBean implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 3158649307217736396L;

    //u
        public String name;
        public String password;
        public Set<UserRole> userRoles;
    //ur
        public UserRolePK id;
        public User user;
    //urpk
        public String roleName="user";
        public int usersId;

        @EJB
        private UserService us;

        //--- getter, setter

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public Set<UserRole> getUserRoles() {
            return userRoles;
        }

        public void setUserRoles(Set<UserRole> userRoles) {
            this.userRoles = userRoles;
        }

        public UserRolePK getId() {
            return id;
        }

        public void setId(UserRolePK id) {
            this.id = id;
        }

        public User getUser() {
            return user;
        }

        public void setUser(User user) {
            this.user = user;
        }

        public String getRoleName() {
            return roleName;
        }

        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }

        public int getUsersId() {
            return usersId;
        }

        public void setUsersId(int usersId) {
            this.usersId = usersId;
        }




        public String addUser()  {

            User u = new User();
            UserRole ur = new UserRole();
            UserRolePK urpk = new UserRolePK();

            u.setName(name);
            u.setPassword(password);
            u.setUserRoles(userRoles);


            ur.setName(name);
            ur.setUser(user);
            ur.setId(urpk);

            urpk.setRoleName(roleName);
            urpk.setUsersId(usersId);

            us.addUser(u, ur);

            return "adduser";
        }
}

用户服务.java:

@Stateless
@LocalBean
public  class UserService implements UserServiceInterface {


    @PersistenceContext(unitName="virtual.trainer.entities", type=PersistenceContextType.TRANSACTION)
    private EntityManager em;

    @Resource
    @ Inject
    private UserTransaction xtx ;

    public UserService() {

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> getUsers() {
//      return null;
        return em.createQuery("FROM User").getResultList();
    }


    public void addUser(User u, UserRole ur)  {



            try {
                xtx.begin();
                em.persist(u);
                em.persist(ur);
                xtx.commit();
            } catch (NotSupportedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Chujnia z grzybnia1/7");
            } catch (SystemException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Chujnia z grzybnia2/7");
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Chujnia z grzybnia3/7");
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Chujnia z grzybnia4/7");
            } catch (RollbackException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Chujnia z grzybnia5/7");
            } catch (HeuristicMixedException e) {
                // TODO Auto-generated catch block  
                e.printStackTrace();
                System.out.println("Chujnia z grzybnia6/7");
            } catch (HeuristicRollbackException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("Chujnia z grzybnia7/7");
            }

//          em.persist(urpk);

            em.flush();
//          em.close();

    }

}

安慰:

07:28:53,476 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) javax.transaction.NotSupportedException: BaseTransaction.checkTransactionState - ARJUNA016051: thread is already associated with a transaction!

07:28:53,478 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.begin(BaseTransaction.java:63)

07:28:53,479 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.begin(BaseTransactionManagerDelegate.java:65)

07:28:53,480 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.begin(ServerVMClientUserTransaction.java:142)

07:28:53,481 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at virtual.trainer.bean.UserService.addUser(UserService.java:66)

07:28:53,482 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

07:28:53,483 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

07:28:53,484 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

07:28:53,484 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at java.lang.reflect.Method.invoke(Unknown Source)

07:28:53,485 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.java:72)

07:28:53,486 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,487 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)

07:28:53,488 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:36)

07:28:53,489 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,490 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47)

07:28:53,491 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,492 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)

07:28:53,493 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,494 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)

07:28:53,495 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)

07:28:53,496 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,497 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)

07:28:53,498 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,499 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:228)

07:28:53,499 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304)

07:28:53,500 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190)

07:28:53,501 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,502 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)

07:28:53,503 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,504 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59)

07:28:53,505 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,506 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)

07:28:53,507 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,508 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:32)

07:28:53,509 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,510 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)

07:28:53,511 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,511 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)

07:28:53,512 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165)

07:28:53,513 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:173)

07:28:53,514 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)

07:28:53,515 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)

07:28:53,516 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:72)

07:28:53,517 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at virtual.trainer.bean.UserService$$$view297.addUser(Unknown Source)

07:28:53,517 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at pl.wlukasiak.beans.RegistrationBean.addUser(RegistrationBean.java:120)

07:28:53,518 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

07:28:53,519 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

07:28:53,520 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

07:28:53,521 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at java.lang.reflect.Method.invoke(Unknown Source)

07:28:53,521 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.el.parser.AstValue.invoke(AstValue.java:262)

07:28:53,522 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)

07:28:53,523 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)

07:28:53,524 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)

07:28:53,525 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)

07:28:53,526 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at javax.faces.component.UICommand.broadcast(UICommand.java:315)

07:28:53,527 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)

07:28:53,527 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)

07:28:53,528 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)

07:28:53,529 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)

07:28:53,530 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)

07:28:53,531 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)

07:28:53,532 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

07:28:53,533 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

07:28:53,533 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

07:28:53,534 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

07:28:53,535 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:397)

07:28:53,536 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)

07:28:53,537 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

07:28:53,538 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

07:28:53,539 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

07:28:53,540 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

07:28:53,541 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

07:28:53,542 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

07:28:53,542 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

07:28:53,543 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

07:28:53,544 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at java.lang.Thread.run(Unknown Source)

07:28:53,545 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) Caused by: java.lang.IllegalStateException: BaseTransaction.checkTransactionState - ARJUNA016051: thread is already associated with a transaction!

07:28:53,546 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.checkTransactionState(BaseTransaction.java:257)

07:28:53,547 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.begin(BaseTransaction.java:59)

07:28:53,548 ERROR [stderr] (http-localhost-127.0.0.1-8080-1)   ... 74 more

07:28:53,549 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Chujnia z grzybnia1/7
4

1 回答 1

0

您不能一次更新两个表。

您可以通过将 vars 传递给 proc 创建一个数据库过程来执行此操作。

使用事务,您可以检查更新结果并继续进行下一次更新。如果其中一个失败,您可以回滚并且不会发生任何更改。

MySql 支持这一点,但您需要使用正确的设置和数据库 [ InnoDB ]。

开始交易;更新表一组 x = bla; if error rollback else 更新表二 set y = bla; 如果错误回滚,否则提交;

http://dev.mysql.com/doc/refman/5.0/en/commit.html

该示例不是代码 - 只是一个示例

您可以用 Java 或更好的方式编写它——避免使用 Java 并编写一个 proc 。 http://dev.mysql.com/doc/refman/5.0/en/stored-routines.html

于 2013-01-09T00:50:10.147 回答