で複数のテーブル (MySQL) にデータを挿入する方法Java EE&JBoss7.1
。いくつかの簡単なチュートリアルを行いましたが、今ではデータを追加した 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;
}
}
UserRole.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;
}
}
UserRolePK.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";
}
}
UserService.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