我在JTA中使用Eclipselink和Wildfly 8,问题是在提交用户事务并将数据插入数据库之后它不会更新相关实体,所以如果我重新加载页面或打开页面的新实例,它会加载没有插入行的数据。
@ManagedBean
@ViewScoped
public class empcreator {
UserTransaction ut=null;
@PersistenceContext(unitName="game")
private EntityManager em;
Context icontext=null;
public empcreator() throws NamingException{
icontext=new InitialContext();
ut=(UserTransaction)icontext.lookup("java:comp/UserTransaction");
}
持久化方法是:
public void addElementToIsland() throws NamingException, NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException{
int objectid=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("objectid"));
int x=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("x"));
int y=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("y"));
int z=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("z"));
int r=Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("r"));
int islid=selectedisland;
Query q=em.createNamedQuery("Element.findOne");
q.setParameter("id", objectid);
Element element=(Element)q.getResultList().get(0);
q=em.createNamedQuery("Island.findOne");
q.setParameter("islandid", islid);
Island isl=(Island)q.getResultList().get(0);
islandsobjects el=new islandsobjects();
el.setElement(element);
el.setX(x);
el.setY(y);
el.setZ(z);
el.setR(r);
el.setIsland(isl);
ut.begin();
em.persist(el);
ut.commit();
}
,加载功能是:
public void loadIsland(ActionEvent e){
Query q=em.createNamedQuery("Island.findOne");
q.setParameter("islandid", selectedisland);
List<islandsobjects> iobjects=((Island)q.getResultList().get(0)).getIobjects();
JsonObjectBuilder jsonOB=Json.createObjectBuilder();
JsonArrayBuilder jsonAB=Json.createArrayBuilder();
for(int i=0;i<iobjects.size();i++){
jsonOB.add("id", iobjects.get(i).getId());
Element element=iobjects.get(i).getElement();
jsonOB.add("name", element.getName());
jsonOB.add("x", iobjects.get(i).getX());
jsonOB.add("y", iobjects.get(i).getY());
jsonOB.add("z", iobjects.get(i).getZ());
jsonOB.add("r", iobjects.get(i).getR());
jsonOB.add("objectid", iobjects.get(i).getObjectid());
jsonOB.add("islandid", selectedisland);
jsonAB.add(jsonOB);
}
JsonObject jsElements=Json.createObjectBuilder().add("elements", jsonAB).build();
RequestContext.getCurrentInstance().execute("loadisland("+jsElements.toString()+")");
}
这是persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="game" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/boddooo</jta-data-source>
<class>boddooo.entity.Empire</class>
<class>boddooo.entity.Empiresobject</class>
<class>boddooo.entity.Island</class>
<class>boddooo.entity.Element</class>
<class>boddooo.entity.User</class>
<class>boddooo.entity.World</class>
<class>boddooo.entity.islandsobjects</class>
<properties>
<property name="eclipselink.target-server" value="JBoss"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.persistence-context.flush-mode" value="auto" />
</properties>
</persistence-unit>
</persistence>
我还将刷新模式更改为commit但没有结果。