我正在寻找类似的问题,但没有实际的结果,所以我试图用JPA / Maven创建简单的DAO层,但是如果我使用它,那么当我使用EntityManager时,只有persist方法(insert)正在工作class,但如果我使用before创建EntityManager并且没有任何其他内容的自动注释,则测试工作正常。我正在获取NullPointerExeption,但我无法理解为什么(特别是为什么持久化方法在这种情况下工作)。
抽象测试
public abstract class AbstractPersistentTest {
protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory("Services");
protected EntityManager em;
protected EntityTransaction tx;
@Before
public void initEntityManager() throws Exception {
em = emf.createEntityManager();
tx = em.getTransaction();
}
@After
public void closeEntityManager() throws SQLException {
if (em != null) em.close();
}
protected Integer getRandomId() {
return Math.abs(new Random().nextInt());
}
}
模型
@Entity
@Table(name = "test")
public class Test implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "title", nullable = false)
@Size(min = 0, max = 255)
private String title;
@Column(name = "descr", nullable = false, length = 16777215, columnDefinition = "Mediumtext")
private String descr;
@Column(name = "avatar", nullable = false)
private String avatar;
@Column(name = "date_add", nullable = false, insertable = false, updatable = false, columnDefinition = "Datetime DEFAULT CURRENT_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date date_add;
public Test() {
}
//getters and setters
测试DAO
public class TestDAO {
//@PersistenceContext(unitName = "Services")
//private EntityManager emgr;
public Integer persist(String title,
String avatar,
String descr) {
EntityManager em = EMgrUtil.createEntityManager();
EntityTransaction transaction = em.getTransaction();
Test test = new Test();
try {
transaction.begin();
test.setTitle(title);
test.setAvatar(avatar);
test.setDescr(descr);
em.persist(test);
transaction.commit();
}
catch(RuntimeException e) {
e.printStackTrace();
transaction.rollback();
} finally {
//em.close();
}
return test.getId();
}
public Test findById(Integer id) {
EntityManager em = EMgrUtil.getEntityManager();
Test t = em.find(Test.class, id);
return t;
}
public List<Test> findAll() {
EntityManager em = EMgrUtil.getEntityManager();
List<Test> list = em.createQuery("SELECT t FROM Test t", Test.class).getResultList();
return list;
}
}
我也尝试通过PersistenceContext注入EntityManager,但它不起作用
主
public class testDAO_test {
public static void main(String[] args) {
TestDAO testDAO = new TestDAO();
List<Test> tests = testDAO.findAll();
for(Test test : tests) {
System.out.println(test);
}
//Test test = testDAO.findById(2);
//System.out.println(test);
}
}
的persistence.xml
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="Services" transaction-type="RESOURCE_LOCAL">
<description>
Maven Test JPA
</description>
<!--provider>org.hibernate.ejb.HibernatePersistence</provider-->
<class>com.spring_test2.jpa.models.Test</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/spring_test2" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="pwd" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
答案 0 :(得分:1)
在getEntityManager()中添加对实体管理器的空检查,就像在createEntityManager()中一样,如果它为null,则创建一个em。
或者只是在TestDAO中使用createEntityManager()而不是getEntityManager()来查找findAll()和findById()方法。