Hibernate - 使用ManyToOne Relationship获取数据

时间:2015-07-02 09:29:09

标签: java hibernate orm jersey jax-rs

Hibernate - 使用ManyToOne Relationship获取数据

父DeviceType实体,

 @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;
}
}

子设备实体,

 @Entity
 @Table(name="device"
 ,catalog="igedb"
 )
 public class Device  implements java.io.Serializable {

 private Integer id;
 private String deviceNumber;
 private DeviceType deviceType;

 public Device() {
 }

@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_number", length=45)   
public String getDeviceNumber() {
    return this.deviceNumber;
}    
public void setDeviceNumber(String deviceNumber) {
    this.deviceNumber = deviceNumber;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="device_type", nullable=false)
public DeviceType getDeviceType() {
    return this.deviceType;
}
public void setDeviceType(DeviceType deviceType) {
    this.deviceType = deviceType;
}
}

获取代码,

deviceList = session.createQuery("FROM Device").list();

WebService代码,

@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;
}

我在调试时显示记录直到“返回响应”声明。

它在WebService响应中提供内部服务器错误500。

请帮助一下吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

可能是懒得多对一

@ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name="device_type", nullable=false)
public DeviceType getDeviceType() {
    return this.deviceType;
}

@OneToMany(fetch=FetchType.LAZY, mappedBy="deviceType")
 public List<Device> getDevices() {
 return this.devices;
 }    

当您通过Web服务发送查询时,您正在向Web服务发送代理。远程无法访问代理中的信息并失败。

您必须提取设备类型以将真实信息发送到网络服务。

@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;
 }  

答案 1 :(得分:0)

您需要将您的列表转换为json字符串,然后您可以像这样构建您的responese。

@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;
}