JPA不会将实体插入数据库

时间:2015-08-05 14:20:01

标签: hibernate jpa transactions ejb

我使用hibernate-jpa-2.1-api

我有一个用于业务逻辑的EJB类

我已经创建了一个注入EJB并在save方法中调用的@RequestScopded EntityManager,没有错误,但数据库中没有条目。有趣的是,在数据库中正确地增加了休眠序列号,但是没有调用实体的插入,我甚至没有在日志中看到实体的插入语句。

如果我打电话

  

em.flush()

之后

  

em.persist()

我收到错误消息,告知没有正在进行的交易。

我为其他实体保存的方法以相同的方式组织并且正常工作。我还尝试将EJB中的EntityManager作为@PersistenceContect注入,在这种情况下它正在工作,但我想知道为什么在这种特定情况下,requesed scoped EM不起作用,而在其他情况下也是如此。

这是我的代码:

1。)实体

@Entity
@Table(name = "CONSULTATION")
public class Consultation implements java.io.Serializable {

private static final long serialVersionUID = 2989793280017282405L;
private long idConsulation;
private DomainExpert domainExpert;
private Date date;
private Long hours;
private String description;
private PayedResource payedResource;
private Integer version;

public Consultation() {
}

public Consultation(long idConsulation, DomainExpert domainExpert) {
    this.idConsulation = idConsulation;
    this.domainExpert = domainExpert;
}

public Consultation(long idConsulation, DomainExpert domainExpert, Date date, Long hours) {
    this.idConsulation = idConsulation;
    this.domainExpert = domainExpert;
    this.date = date;
    this.hours = hours;
}

@Id
@TableGenerator(name = "ID_CONSULATION", table = "hibernate_sequence", pkColumnName = "SEQUENCE_NAME", valueColumnName = "NEXT_VAL", pkColumnValue = "SEQ_ID_CONSULATION", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "ID_CONSULATION")
@Column(name = "ID_CONSULATION", unique = true, nullable = false)
public long getIdConsulation() {
    return this.idConsulation;
}

public void setIdConsulation(long idConsulation) {
    this.idConsulation = idConsulation;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_DOMAIN_EXPERT", nullable = false)
public DomainExpert getDomainExpert() {
    return this.domainExpert;
}

public void setDomainExpert(DomainExpert domainExpert) {
    this.domainExpert = domainExpert;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DATE", length = 19, nullable = false)
public Date getDate() {
    return this.date;
}

public void setDate(Date date) {
    this.date = date;
}

@Column(name = "HOURS", length = 20, nullable = false)
public Long getHours() {
    return this.hours;
}

public void setHours(Long hours) {
    this.hours = hours;
}

@Column(name = "DESCRIPTION", length = 500)
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@Version
@Column(name = "VERSION", nullable = false)
public Integer getVersion() {
    return this.version;
}

public void setVersion(Integer version) {
    this.version = version;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_PAYED_RESOURCE", nullable = false)
public PayedResource getPayedResource() {
    return this.payedResource;
}

public void setPayedResource(PayedResource payedResource) {
    this.payedResource = payedResource;
}

}

2。)EJB

@Stateless
public class DomainExpertHandler {

    @Inject
    private EntityManager em;

    public void saveConsultation(Consultation consultation) {
       if (consultation.getIdConsulation() > 0) {
         em.merge(consultation);
       } else {
         em.persist(consultation);
       }
    }
}

3。)生成@requestscoped EM的bean

public class Resources {

    @PersistenceUnit
    EntityManagerFactory emf;

    @Produces
    @RequestScoped
    public EntityManager createEntityManager() {
        return emf.createEntityManager();
    }

    protected void closeEntityManager(@Disposes EntityManager entityManager) {

        if (entityManager.isOpen()) {
            entityManager.close();
        }
    }


}

4.。)我的persistence.xml

<persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

   <persistence-unit  name="cloudflow" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:jboss/datasources/cloudflow</jta-data-source>
        <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.jdbc.batch_size" value="50"/>  
         <property name="hibernate.show_sql" value="false" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />  
        </properties>
   </persistence-unit>

</persistence>

1 个答案:

答案 0 :(得分:0)

您可以手动定义交易吗?

EntityTransaction trans = em.getTransaction();
    trans.begin();
    //etc....

如果没有,请尝试

@PersistenceContext(name="cloudflow")
    EntityManager em;

并查看this answer