Java EE 7 / EJB / JPA - 实体管理器在持久化实体时不创建事务

时间:2014-03-11 00:38:34

标签: jpa transactions ejb entitymanager

我有一个客户端应用程序:

public static void main(String[] args) throws NamingException {
    Properties p = new Properties();
    p.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
    p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");        
    InitialContext ctx = new InitialContext(p);

    ServiceEJBStatelessRemote service = (ServiceEJBStatelessRemote)ctx.lookup("ServiceEJBStatelessJNDI#com.marcos.service.remote.ServiceEJBStatelessRemote");

    User user = new User(123, "Foo BAR", "ertddeew45", "foousername");

    Long id = service.insertUser(user);     
    System.out.println("User has been created with id: "+id);

每当我尝试将此实体持久保存在数据库中时,我都没有例外但是没有存储在我的数据库中。

在我的服务(无状态EJB)中,我有两个方法,一个用于插入,另一个用于检索;检索方法工作正常,所以我可以看到实体管理器属性工作,我的持久单元以及我可以去数据库并检索我正在寻找的值。

但是当我调用insertUser()方法时,我没有得到任何错误,但是实体没有保存在DB中,就像没有提交动作一样。

  • 我在Weblogic 12c服务器中部署我的应用程序
  • 为了运行我的客户端应用程序,我将wlthint3client.jar添加到我的构建路径中
  • 在我的insertUser()方法中,我无法调用getTransaction()cos,然后我得到一个异常。

我错过了什么吗?

这是我的服务类:

@Stateless(mappedName = "ServiceEJBStatelessJNDI", name="ServiceEJBStateless")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ServiceEJBStateless implements ServiceEJBStatelessRemote {

@PersistenceContext(unitName="Test2Jpa2")
EntityManager em;

public ServiceEJBStateless() {
}

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public long insertUser(User user) {
    em.persist(user);
    em.merge(user);
    return user.getUserId();
}

 @Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public User findUser(long id) {
    return em.find(User.class, id);
}

这是我的persistance.xml

<persistence-unit name="Test2Jpa2" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.marcos.domain.User</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/>
        <property name="javax.persistence.jdbc.user" value="hibernatetest"/>
        <property name="javax.persistence.jdbc.password" value="pwd123"/>
    </properties>
</persistence-unit>

这是我的实体:

@Entity
@Table(name="USERS")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="USER_ID")
private long userId;

private String fullname;

private String password;

private String username;

public User() {
}

public User(String fm, String pwd, String username) {
    this.fullname = fm;
    this.password = pwd;
    this.username = username;
}

public User(long id, String fm, String pwd, String username) {
    this.userId = id;
    this.fullname = fm;
    this.password = pwd;
    this.username = username;
}

public long getUserId() {
    return this.userId;
}

public void setUserId(long userId) {
    this.userId = userId;
}

public String getFullname() {
    return this.fullname;
}

public void setFullname(String fullname) {
    this.fullname = fullname;
}

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

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

public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

@Override
public String toString(){
    return "Id: "+this.userId+"\n"+
            "Name: "+this.fullname+"\n"+
            "Username: "+this.username+"\n"+
            "Password: "+this.password;
}

0 个答案:

没有答案