我在下面发布我所做的一切,所以请看一下。
我不知道错误在哪里,并且显示未知实体错误。我什至在休眠配置文件(休眠.cfg.xml(中映射了一个模型
REST 中的资源类:
package com.tss.friends_api.user.resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import com.tss.friends_api.user.model.User;
import com.tss.friends_api.user.service.UserService;
import com.tss.friends_api.user.service.UserServiceImpl;
@Path("user")
public class UserResource {
UserService userService=new UserServiceImpl();
@Path("/test")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String createUser(){
return "user";
}
@POST
@Path("")
@Consumes(MediaType.APPLICATION_JSON)
public Response addUser(User user,@Context UriInfo uriInfo) throws Exception{
if(user==null) throw new Exception();
System.out.println(user);
String string=userService.addUser(user);
if(string!=null) {
return Response.created(uriInfo.getAbsolutePathBuilder().path(user.getUserId()+"").build()).entity("account created").build();
}
return null;
}
型号类别:
package com.tss.friends_api.user.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="user_id",nullable=false)
private int userId;
@Column(name="user_name")
private String userName;
@Column(name="user_emailid")
private String userEmailId;
@Column(name="user_contactnumber")
private long userContactNumber;
@Column(name="user_password")
private String userPassword;
@OneToMany(fetch=FetchType.LAZY,targetEntity=Friends.class,cascade=CascadeType.ALL)
private List<Friends> userFriends;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserEmailId() {
return userEmailId;
}
public void setUserEmailId(String userEmailId) {
this.userEmailId = userEmailId;
}
public long getUserContactNumber() {
return userContactNumber;
}
public void setUserContactNumber(long userContactNumber) {
this.userContactNumber = userContactNumber;
}
public List<Friends> getUserFriends() {
return userFriends;
}
public void setUserFriends(List<Friends> userFriends) {
this.userFriends = userFriends;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
Hibernate.cgx.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/restws</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">anil4100</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping package="com.tss.friends_api.user.model"/>
</session-factory>
</hibernate-configuration>
用户服务类:
package com.tss.friends_api.user.service;
import com.tss.friends_api.user.dao.UserDao;
import com.tss.friends_api.user.dao.UserDaoImpl;
import com.tss.friends_api.user.model.User;
public class UserServiceImpl implements UserService {
UserDao userDao=new UserDaoImpl();
@Override
public String addUser(User user) {
return userDao.addUser(user);
}
用户道类:
package com.tss.friends_api.user.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.tss.friends_api.user.model.User;
public class UserDaoImpl implements UserDao {
private SessionFactory sessionFactory;
public UserDaoImpl() {
Configuration configuration=new Configuration();
configuration.configure();
sessionFactory=configuration.buildSessionFactory();
}
@Override
public String addUser(User user) {
Session session=sessionFactory.openSession();
return session.save(user)+"";
}
最后是错误(简称(:
org.hibernate.MappingException: 未知实体: com.tss.friends_api.user.model.User
添加
sessionFactory.setPackagesToScan(new String[] { "com.tss.friends_api.user.model" });
到你在UserDaoImpl
的sessionFactory
.
我可以想象你的Hibernate.cgx.xml
被忽略了,因为你在UserDaoImpl
中建立了自己的sessionFactory
。
如果你正在使用春天,看看 这里 .