Hibernate 休眠一对多返回arrayindexoutofbounds
实体类包括: DeviceWithReading.javaHibernate 休眠一对多返回arrayindexoutofbounds,hibernate,Hibernate,实体类包括: 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
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;
}
}
各表如下:
装置
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
)
设备\u最新\u读数
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());
如果代码中有任何错误,请告诉我。谢谢你的帮助 您必须在实体中声明@Id字段,以使多通关联正常工作:
@Id
@Column(name="id")
@GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
对不起,我的答复太晚了。谢谢你的回复。这就是我工作的方式 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;
}
清单的代码是
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();
标准criteriaObj=null;
设备配置和ADINGSVO设备,读取VO=null;
尝试
{
criteriaObj=MyDBUtil.getCriteriaWithAlias(DeviceWithReading.class,“deviceAndReadings”);
}捕获(ListenerApplicationException e)
{
投掷e;
}
列表DEVWITHREADS=标准对象
.createAlias(“deviceReadings”、“dr”、标准规范。左联合)
.setResultTransformer(标准.DISTINCT\u ROOT\u实体)
.add(Restrictions.eq(“id”,新的Long(deviceId)))
.list();
无法集成resulttransformer以生成VO,因此我自己在VO中迭代并设置数据。有些字段不在表中,但在我的BaseMasterEntity.java类(由etc修改创建)中使用,这是一个问题。我通过更改DeviceLatestData的表定义也解决了这个问题
再次感谢为什么有两个setResultTransformer?一个是因为我不想重复读取值的根设备。另一个是因为我需要在值对象中获得结果。我已尝试删除VO,但相同的例外情况是第二个替换第一个,因为您一次只能有一个活动的
ResultTransformer
(方法是setResultTransformer()
,而不是addResultTransformer()
)。即使在删除另一个并只留下criteriaObj.setResultTransformer(Criteria.DISTINCT\u ROOT\u实体)之后,也可以尝试使用new HashSet()
初始化您的deviceReadings
;结果是所有.property()
的sametry设置.as()
,您实际上不需要@Column(name=“id”)
@Boris brodski:True,True。。谢谢你指出!为了更好地理解真实发生的事情,我编写了明确的版本。
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();