我调用userDao.findById(整数I)并给出java.lang.NullPointerException我给出的是db中的确切id
这是我的密码:我调用userDao.findById(整数I)并给出java.lang.NullPointerException我给出的是db中的确切id,java,hibernate,jpa,Java,Hibernate,Jpa,这是我的密码: public class Test_JPA { public static void main(String[] args) { UserDao uDao = new UserDaoImplement(); User u = uDao.findById(4); System.out.println(u.getName()); } } 在myUserDaoImplement()上 这是我的persistence.
public class Test_JPA {
public static void main(String[] args) {
UserDao uDao = new UserDaoImplement();
User u = uDao.findById(4);
System.out.println(u.getName());
}
}
在myUserDaoImplement()上代码>
这是我的persistence.xml
<persistence-unit name="WebApp"
transaction-type="RESOURCE_LOCAL"
>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.model.Profile</class>
<class>com.model.User</class>
<properties>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!-- <property name="hibernate.current_session_context_class" value="thread"/>
-->
<!-- per hibernate 4.3 -->
<!-- <property name="hibernate.currrent_session_context_class" value="org.hibernate.context.internal.ThreadLocalSessionContext"/>
-->
</properties>
</persistence-unit>
org.hibernate.ejb.HibernatePersistence
com.model.Profile
com.model.User
对不起,忘记了用户实体
@实体
@桌子
公共类用户
{
}UserDaoImplement.java中第85行的内容。我愿意打赌它是整个管理者。您似乎已经完成了UserDao uDao=newuserdaoomplement()代码>这是您的测试用例。您对私有EntityManager EntityManager的期望是什么
要在测试用例中填充uDao
实例?请访问User
实体。另外,请添加完整的堆栈跟踪。2015年2月15日下午6:28:34 com.dao.impl.userdaoomplement findById严重:在com.dao.impl.userdaoomplement.findById(userdaoomplement.java:85)获取失败的java.lang.NullPointerException在com.test_jpa.test_jpa.main(test_jpa.java:14)线程“main”中的异常java.lang.NullPointerException在com.dao.impl.UserDaoImplement.findById(UserDaoImplement.java:85)在com.test_jpa.test_jpa.main(test_jpa.java:14)中的异常,那么您得到了一个NPE,那么什么是null呢?是实体经理吗?那么为什么它是空的呢?实际上,我认为@PersistanceContext不需要spring模块,因为我是JPA和hibernate的新手;这是第85行。我只是给出了您正在使用entitymanager的注释@PersistenceContext
。必须有人读取该注释并将PersistenceContext注入entitymanager变量中。通常,Spring或依赖项注入框架会这样做。但是当您执行newuserdaoomplement()
时,谁负责注入PersistenceContext?答案是没有人。这就是为什么会出现空指针异常。非常感谢:)如果没有spring,我只需要创建带有持久性单元的EntityManagerFactory?不。请阅读有关spring的“集成测试”的内容。您可以将spring上下文注入测试用例中,然后按原样使用Springbean,而无需任何new
调用。
<persistence-unit name="WebApp"
transaction-type="RESOURCE_LOCAL"
>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.model.Profile</class>
<class>com.model.User</class>
<properties>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!-- <property name="hibernate.current_session_context_class" value="thread"/>
-->
<!-- per hibernate 4.3 -->
<!-- <property name="hibernate.currrent_session_context_class" value="org.hibernate.context.internal.ThreadLocalSessionContext"/>
-->
</properties>
</persistence-unit>
private Integer id;
private Profile profile;
private String name;
private String email;
private String username;
private String password;
public User() {
}
public User(Profile profile, String name, String email, String username,
String password) {
this.profile = profile;
this.name = name;
this.email = email;
this.username = username;
this.password = password;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id")
public Profile getProfile() {
return this.profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
@Column(name = "name", length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "email", length = 45)
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name = "username", length = 20)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "password", length = 20)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}