实体类是: DeviceWithReading.java
package com.fde.entity;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import com.fde.entity.base.BaseMasterEntity;
@Entity
@Table(name="device")
public class DeviceWithReading extends BaseMasterEntity
{
private String name;
private String deviceIdentifier;
private Integer activePorts;
private Exchange exchange;
private String outputPath;
private Integer iterationTime;
private Boolean monitoring;
private Boolean isConfigured;
private Set<DeviceLatestData> deviceReadings;
/**
* @return the name
*/
/**
* @return
*/
@Column(name="name")
public String getName()
{
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the deviceIdentifier
*/
@Column(name="device_identifier", nullable=false)
public String getDeviceIdentifier()
{
return deviceIdentifier;
}
/**
* @param deviceIdentifier
* the deviceIdentifier to set
*/
public void setDeviceIdentifier(String deviceIdentifier)
{
this.deviceIdentifier = deviceIdentifier;
}
/**
* @return the activePorts
*/
@Column(name="no_of_ports", nullable=false)
public Integer getActivePorts()
{
return activePorts;
}
/**
* @param activePorts
* the activePorts to set
*/
public void setActivePorts(Integer activePorts)
{
this.activePorts = activePorts;
}
/**
* @return
*/
@ManyToOne( )
@JoinColumn(name="exchange_id")
public Exchange getExchange()
{
return exchange;
}
/**
* @param exchangeId
*/
public void setExchange(Exchange exchange)
{
this.exchange = exchange;
}
/**
* @return
*/
@Column(name="file_path")
public String getOutputPath()
{
return outputPath;
}
/**
* @param outputPath
*/
public void setOutputPath(String outputPath)
{
this.outputPath = outputPath;
}
/**
* @return
*/
@Column(name="iteration_time", nullable=false)
public Integer getIterationTime()
{
return iterationTime;
}
/**
* @param iterationTime
*/
public void setIterationTime(Integer iterationTime)
{
this.iterationTime = iterationTime;
}
/**
* @return
*/
@Column(name="device_monitoring", nullable=false)
public Boolean getMonitoring()
{
return monitoring;
}
/**
* @param monitoring
*/
public void setMonitoring(Boolean monitoring)
{
this.monitoring = monitoring;
}
/**
* @return
*/
@Column(name="configured", nullable=false)
public Boolean getIsConfigured()
{
return isConfigured;
}
/**
* @param isConfigured
*/
public void setIsConfigured(Boolean isConfigured)
{
this.isConfigured = isConfigured;
}
/**
* @return the deviceReadings
*/
@OneToMany(mappedBy= "device", fetch = FetchType.LAZY)
@Cascade(CascadeType.SAVE_UPDATE)
public Set<DeviceLatestData> getDeviceReadings()
{
return deviceReadings;
}
/**
* @param deviceReadings the deviceReadings to set
*/
public void setDeviceReadings(Set<DeviceLatestData> deviceReadings)
{
this.deviceReadings = deviceReadings;
}
}
DeviceLatestData.java:
package com.fde.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.fde.entity.base.BaseMasterEntity;
@Entity
@Table(name="device_latest_reading")
public class DeviceLatestData extends BaseMasterEntity
{
private DeviceWithReading device;
private int portId;
private int distance;
/**
* @return the deviceId
*/
/*@ManyToOne(fetch = FetchType.LAZY)*/
@ManyToOne()
@JoinColumn(name = "device_id", nullable = true)
public DeviceWithReading getDevice()
{
return device;
}
/**
* @param deviceId the deviceId to set
*/
public void setDevice(DeviceWithReading deviceId)
{
this.device = deviceId;
}
/**
* @return the portId
*/
@Column(name="port_id", nullable = false)
public int getPortId()
{
return portId;
}
/**
* @param portId the portId to set
*/
public void setPortId(int portId)
{
this.portId = portId;
}
/**
* @return the distance
*/
@Column(name="distance")
public int getDistance()
{
return distance;
}
/**
* @param distance the distance to set
*/
public void setDistance(int distance)
{
this.distance = distance;
}
}
表格是:
设备
CREATE TABLE `device` (
`id` INT(11) NOT NULL,
`name` VARCHAR(45) NULL DEFAULT NULL,
`device_identifier` VARCHAR(45) NOT NULL,
`no_of_ports` INT(11) NOT NULL,
`exchange_id` INT(11) NOT NULL,
`file_path` VARCHAR(250) NULL DEFAULT NULL,
`device_monitoring` TINYINT(1) NULL DEFAULT '1',
`configured` TINYINT(1) NULL DEFAULT NULL,
`iteration_time` INT(11) NOT NULL,
`version` INT(11) NULL DEFAULT NULL,
`createdby` INT(11) NULL DEFAULT NULL,
`modifiedby` INT(11) NULL DEFAULT NULL,
`createddate` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`modifieddate` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `FK_DEVICE_EXCHANGE` (`exchange_id`),
CONSTRAINT `FK_DEVICE_EXCHANGE` FOREIGN KEY (`exchange_id`) REFERENCES `exchange` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
device_latest_reading
CREATE TABLE `device_latest_reading` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`device_id` INT(11) NOT NULL,
`port_id` INT(11) NOT NULL,
`distance` INT(11) NOT NULL,
`createddate` TIMESTAMP NULL DEFAULT NULL,
`modifieddate` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `device_id` (`device_id`),
CONSTRAINT `device_id` FOREIGN KEY (`device_id`) REFERENCES `device` (`id`)
)
当我运行下面的代码时。我得到的集合无法初始化异常。
DeviceWithReading devReadings = (DeviceWithReading) MyDBUtil.getSessionFactory().getCurrentSession().get(DeviceWithReading.class, new Long(deviceId));
DeviceConfigurationAndReadingsVO deviceWithReadingVo = new DeviceConfigurationAndReadingsVO();
deviceWithReadingVo.setActivePorts(devReadings.getActivePorts());
deviceWithReadingVo.setExchange(devReadings.getExchange());
deviceWithReadingVo.setIsConfigured(devReadings.getIsConfigured());
deviceWithReadingVo.setMonitoring(devReadings.getMonitoring());
deviceWithReadingVo.setOutputPath(devReadings.getOutputPath());
deviceWithReadingVo.setReadingFrequency(devReadings.getIterationTime());
deviceWithReadingVo.setDeviceReadings(devReadings.getDeviceReadings());
System.out.println(":::"+ devReadings.getDeviceReadings().toArray());
如果代码中有任何错误,请告诉我。帮助赞赏!
答案 0 :(得分:3)
您必须在实体中声明@Id字段才能使ManyToOne关联起作用:
@Id
@Column(name="id")
@GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
答案 1 :(得分:0)
很抱歉在我的回复中迟到了。感谢您的所有回复。这就是我使它发挥作用的方式 DeviceWithReading.java有
private Set<DeviceLatestData> deviceReadings = new HashSet<DeviceLatestData>();
@OneToMany(mappedBy= "device", fetch = FetchType.EAGER)
@Cascade(CascadeType.SAVE_UPDATE)
public Set<DeviceLatestData> getDeviceReadings()
{
return deviceReadings;
}
DeviceLatestData
private DeviceWithReading device;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "device_id" , nullable = false)
public DeviceWithReading getDevice()
{
return device;
}
列出的代码是
Criteria criteriaObj = null;
DeviceConfigurationAndReadingsVO deviceWithReadingVo = null;
try
{
criteriaObj = MyDBUtil.getCriteriaWithAlias(DeviceWithReading.class,"deviceAndReadings");
} catch (ListenerApplicationException e)
{
throw e;
}
List<DeviceWithReading> devWithReadings = criteriaObj
.createAlias("deviceReadings", "dr", CriteriaSpecification.LEFT_JOIN)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(Restrictions.eq("id", new Long(deviceId)))
.list();
无法为VO生成集成resulttransformer,因此我自己在VO中迭代并设置数据。有一些问题,表中没有一些字段但在我的BaseMasterEntity.java类中使用(由etc修改创建)我通过更改DeviceLatestData的表定义解决了这个问题。
再次感谢