这是我的代码:
public class Test_JPA {
public static void main(String[] args) {
UserDao uDao = new UserDaoImplement();
User u = uDao.findById(4);
System.out.println(u.getName());
}
}
在UserDaoImplement();
@Stateless
public class UserDaoImplement implements UserDao{
private static final Log log = LogFactory.getLog(UserDaoImplement.class);
@PersistenceContext
private EntityManager entityManager;
public User findById(Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = entityManager.find(User.class, id);
log.debug("get successful");
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
这是我的persistance.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>
答案 0 :(得分:0)
抱歉忘记了用户实体
@Entity @表 公共类用户 {
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;
}
}
答案 1 :(得分:0)
UserDaoImplement.java第85行是什么我愿意打赌它是实体经理。你似乎已经完成了UserDao uDao = new UserDaoImplement();
我的测试用例。您希望private EntityManager entityManager;
如何填充测试用例中的uDao
实例?