我是JPA和Servlets的新手。我试图创建学生实体并使用Servlet将其插入我的数据库。代码中没有错误。当我去Servlet时,它在浏览器中没有错误。但是这些值没有插入到我的数据库中。
学生实体: -
@Entity
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Student)) {
return false;
}
Student other = (Student) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "model.Student[ id=" + id + " ]";
}
}
我的Servlet: -
public class A extends HttpServlet {
@PersistenceUnit(unitName = "persistenceCheckPU")
EntityManagerFactory emf;
@Resource
UserTransaction ut;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
Student student = new Student();
student.setName("John");
student.setAge(22);
EntityManager em = emf.createEntityManager();
ut.begin();
em.persist(student);
ut.commit();
out.println("<h2>Success!!!</h2>");
} catch (SecurityException | IllegalStateException | javax.transaction.RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException | NotSupportedException ex) {
Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
}
}
我的目标: -
已更新
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="persistenceCheckPU" transaction-type="JTA">
<jta-data-source>jdbc_persistence</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
答案 0 :(得分:2)
检查您是否有JTA持久性单元(在persistence.xml
中),然后在开始交易后致电em.joinTransaction()
或在开始交易后致电EntityManager em = emf.createEntityManager();
。
答案 1 :(得分:0)
您将需要与该操作相关联的交易。 有两种方法可以解决这个问题 - 1.使用Spring Transaction 添加此注释
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
在您的DAO课程中使用@Transactional
配置您的建议
<aop:config>
<aop:pointcut id="appControllerTransactionPointCuts"
expression="execution(* package.structure..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="appControllerTransactionPointCuts" />
</aop:config>
现在您的DAO将在事务中运行