Java Hibernate-使用多通关系获取数据
Hibernate-使用多通关系获取数据 父设备类型实体Java Hibernate-使用多通关系获取数据,java,hibernate,orm,jersey,jax-rs,Java,Hibernate,Orm,Jersey,Jax Rs,Hibernate-使用多通关系获取数据 父设备类型实体 @Entity @Table(name="device_type" ,catalog="igedb" ) public class DeviceType implements java.io.Serializable { private Integer id; private String deviceType; private String deviceCategory; private List<De
@Entity
@Table(name="device_type"
,catalog="igedb"
)
public class DeviceType implements java.io.Serializable {
private Integer id;
private String deviceType;
private String deviceCategory;
private List<Device> devices = new ArrayList<Device>();
public DeviceType() {
}
@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;
}
@Column(name="device_type", length=45)
public String getDeviceType() {
return this.deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
@Column(name="device_category", length=45)
public String getDeviceCategory() {
return this.deviceCategory;
}
public void setDeviceCategory(String deviceCategory) {
this.deviceCategory = deviceCategory;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="deviceType")
public List<Device> getDevices() {
return this.devices;
}
public void setDevices(List<Device> devices) {
this.devices = devices;
}
}
获取代码
deviceList = session.createQuery("FROM Device").list();
Web服务代码
@GET
@Path("devices")
@Produces(MediaType.APPLICATION_JSON)
public Response getDeviceList(){
Response response = null;
List<Device> deviceList = null;
try{
DeviceManager deviceMgr = new DeviceManager();
deviceList = deviceMgr.getDeviceList();
GenericEntity<List<Device>> list= new GenericEntity<List<Device>> (deviceList) {};
response = Response.ok(list).build();
} catch(Exception exec){
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Internal server error").build();
}
return response;
}
@GET
@路径(“设备”)
@产生(MediaType.APPLICATION_JSON)
公共响应getDeviceList(){
响应=空;
列表设备列表=null;
试试{
DeviceManager deviceMgr=新的DeviceManager();
deviceList=deviceMgr.getDeviceList();
GenericeEntity列表=新的GenericeEntity(deviceList){};
response=response.ok(list.build();
}捕获(异常执行){
response=response.status(response.status.INTERNAL\u SERVER\u错误)
.entity(“内部服务器错误”).build();
}
返回响应;
}
当我调试时,它显示记录直到“returnresponse”语句
它在WebService响应上给出内部服务器错误500
你能帮个忙吗
提前谢谢。可能是懒惰的多对一
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="device_type", nullable=false)
public DeviceType getDeviceType() {
return this.deviceType;
}
及
及
@OneToMany(fetch=FetchType.EAGER,mappedBy=“deviceType”)
公共列表getDevices(){
归还此设备;
}
您需要将列表转换为json字符串,然后可以像这样构建响应
@GET
@Path("devices")
@Produces(MediaType.APPLICATION_JSON)
public Response getDeviceList(){
Response response = null;
List<Device> deviceList = null;
try{
DeviceManager deviceMgr = new DeviceManager();
deviceList = deviceMgr.getDeviceList();
GenericEntity<List<Device>> list= new GenericEntity<List<Device>> (deviceList) {};
String jsonResult = new Gson().toJson( list );
response = Response.ok(jsonResult).build();
} catch(Exception exec){
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Internal server error").build();
}
return response;
}
@GET
@路径(“设备”)
@产生(MediaType.APPLICATION_JSON)
公共响应getDeviceList(){
响应=空;
列表设备列表=null;
试试{
DeviceManager deviceMgr=新的DeviceManager();
deviceList=deviceMgr.getDeviceList();
GenericeEntity列表=新的GenericeEntity(deviceList){};
字符串jsonResult=new Gson().toJson(列表);
response=response.ok(jsonResult.build();
}捕获(异常执行){
response=response.status(response.status.INTERNAL\u SERVER\u错误)
.entity(“内部服务器错误”).build();
}
返回响应;
}
我想你需要发布更多的代码才能得到明确的答案,包括设备
实体的代码。为实体添加了两个类我试图添加FetchType,但它给出了相同的错误。仍然给出了相同的错误。我把双方都改为渴望。编辑了更多细节。添加了webservice部分
@OneToMany(fetch=FetchType.LAZY, mappedBy="deviceType")
public List<Device> getDevices() {
return this.devices;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="device_type", nullable=false)
public DeviceType getDeviceType() {
return this.deviceType;
}
@OneToMany(fetch=FetchType.EAGER, mappedBy="deviceType")
public List<Device> getDevices() {
return this.devices;
}
@GET
@Path("devices")
@Produces(MediaType.APPLICATION_JSON)
public Response getDeviceList(){
Response response = null;
List<Device> deviceList = null;
try{
DeviceManager deviceMgr = new DeviceManager();
deviceList = deviceMgr.getDeviceList();
GenericEntity<List<Device>> list= new GenericEntity<List<Device>> (deviceList) {};
String jsonResult = new Gson().toJson( list );
response = Response.ok(jsonResult).build();
} catch(Exception exec){
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Internal server error").build();
}
return response;
}