英文:
how to insert to table with JTA?
问题
我想将数据持久化到表中。当我调用保存方法时,它抛出一个异常:"**EXCEPTION saveToDB ---java.lang.IllegalStateException: Transaction is not accessible when using JTA with JPA-compliant transaction access enabled**"。
我尝试使用以下代码片段来解决这个问题,但没有成功:
```java
@Resource
private UserTransaction transaction;
public void method() {
try {
transaction.begin();
...进行持久化操作...
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
}
在持久化实体时,如何避免出现给定的异常?
这是我的保存方法:
private void saveReportToDB(String id, String name, String data, String mode) {
try {
logger.info(" --- 开始 saveToDB ---");
entityManager.getTransaction().begin();
Template report = new Template();
report.setId(id);
report.setName(name);
report.setStatus(ReportStatus.ACTIVE);
report.setTemplateContent(data); // 抛出异常
report.setTemplateDscx(name);
report.setTplCode(mode);
report.setCommitId(id);
entityManager.persist(report);
entityManager.getTransaction().commit();
} catch (Exception ex) {
logger.info(" --- 异常 saveToDB ---" + ex);
}
}
这是我的实体类(部分内容):
@Entity
@Table(name = "TEMPLATES")
public class Template {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
@Column(name = "name", length = 128)
private String name;
@Column(name = "code", length = 24, unique = true)
private String code;
@Lob
@Column(name = "content")
private String content;
@Column(name = "dscx")
private String dscx;
英文:
I wanna to persist data to table. When I call the save method, it throws an exception: "EXCEPTION saveToDB ---java.lang.IllegalStateException: Transaction is not accessible when using JTA with JPA-compliant transaction access enabled"
I've tried to use the following snippet to fix the issue, with no success:
@Resource
private UserTransaction transaction;
public method() {
try {
transaction.begin();
...do some persistence...
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
}
How to avoid the given exception when persisting the entity?
This is my save method:
private void saveReportToDB(String id, String name, String data,String mode) {
try {
logger.info("--- START saveToDB ---");
entityManager.getTransaction().begin();
Template report = new Template();
report.setId(id);
report.setName(name);
report.setStatus(ReportStatus.ACTIVE);
report.setTemplateContent(data);//throws Exception
report.setTemplateDscx(name);
report.setTplCode(mode);
report.setCommitId(id);
entityManager.persist(report);
entityManager.getTransaction().commit();
}catch (Exception ex){
logger.info("--- EXEPTON saveToDB ---"+ex);
}
}
And this is my entity (partially):
@Entity
@Table(name = "TEMPLATES")
public class Template {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
@Column(name = "name", length = 128)
private String name;
@Column(name = "code", length = 24, unique = true)
private String code;
@Lob
@Column(name = "content")
private String content;
@Column(name = "dscx")
private String dscx;
答案1
得分: 1
如果您正在使用容器管理的事务 - 正如情况所示 - 那么没有理由注入UserTransaction对象或使用entityManager#getTransaction()方法。您只需在CDI的@Transactional注释方法或EJB的@TransactionAttribute注释方法内部执行该方法。
简而言之:您只需在saveReportToDB方法内部删除事务调用,让CDI或EJB事务控制为您解决问题。
如果您确实需要控制事务,您必须对bean进行注释,以便事务控制由bean控制(而不是容器控制),这样您就可以使用注入的UserTransaction。
英文:
If you are using a container managed transaction - as it appears to be the case -, there is no reason to inject an UserTransaction object or in using entityManager#getTransaction() methods. You should only execute the method inside a CDI @Transactional annotated method or an EJB @TransactionAttribute annotated method.
In short: you just need to remove your transaction calls inside your saveReportToDB method and let CDI or EJB transaction control solve the problem for you.
If you really need to control transaction, you must annotate your bean so the transaction control is bean controlled (instead of container controlled), so you will be able to use the injected UserTransaction.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论