Hibernate returns ArrayIndexOutofbounds

The entity class is:
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 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
*/
pu blic 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;
}< br />
/**
* @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 getDeviceReadings()
{< br /> 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 DeviceLatestDa ta 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;
}
< br />
}

The table is:

Device

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',< br /> `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`),
C ONSTRAINT `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`)
)

When I run the code below. I received a collection cannot be initialized exception.

DeviceWithReading devReadings = (DeviceWithReading) MyDBUtil.getSessionFactory().getCurrentSession( ).get(DeviceWithReading.class, new Long(deviceId));
DeviceConfigurationAndReadingsVO deviceWithReadingVo = new DeviceConfigurationAndReadingsVO();
deviceWi thReadingVo.setActivePorts(devReadings.getActivePorts());
deviceWithReadingVo.setExchange(devReadings.getExchange());
deviceWithReadingVo.setIsConfigured(devReadings.getIsConfigured());
deviceWithReadingVo.ReadingMonitoring(devReadings.getExchange()); .getMonitoring());
deviceWithReadingVo.setOutputPath(devReadings.getOutputPath());
deviceWithReadingVo.setReadingFrequency(devReadings.getIterationTime());
deviceWithReadingVo.setDeviceReadings(devReadings.get)DeviceReadings() ;
System.out.println(":::"+ devReadings.getDeviceReadings().toArray());

If there are any errors in the code, please let me know. Help appreciated!

You must declare the @Id field in the entity for the ManyToOne association to work:

< /p>

@Id
@Column(name="id")
@GeneratedValue
public Long getId() {return id; }

public void setId(Long id) {this.id = id; }

The entity class is:
DeviceWithReading. java

< /p>

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 deviceReadings;

/**
* @return the name
*/
/**< br /> * @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)
p ublic 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;
}< br />
/**
* @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)
{< br /> 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;
}


< br /> /**
* @return
*/
@Column(name="configured", nullable=false)
public Boolean getIsConfigured()
{
return isConfigured;
}

/**
* @pa ram isConfigured
*/
public void setIsConfigured(Boolean isConfigured)
{
this.isConfigured = isConfigured;
}

/**
* @return the deviceReadings
*/
@OneToMany(mappedBy= "device", fetch = FetchType.LAZY)
@Cascade(CascadeType.SAVE_UPDATE)
public Set getDeviceReadings()
{
return deviceReadings;
}

/**
* @param deviceReadings the deviceReadings to set
*/
public void setDeviceReadings(Set deviceReadings)
{
this.deviceReadings = deviceReadings;
}

}

DeviceLatestData.java:

package com.fde.entity;

import javax.persistence.Column;
import javax. persistence.Entity;
import javax.persistence.FetchType;
impo rt 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;
}< br />/**
* @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< br /> */
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;
}


}

The table is:

Equipment

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`)
)

When I run the following code. I receive a collection cannot be initialized exception.

DeviceWithReading devReadings = (DeviceWithR eading) 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());

If there are any errors in the code, please let me know. Help appreciated!

You must declare the @Id field in the entity to make the ManyToOne association work:

@Id
@Column(name="id")
@GeneratedValue
public Long getId() {return id; }

public void setId(Long id ) {this.id = id; }

Leave a Comment

Your email address will not be published.