Reputation: 845
The entity classes are: 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;
}
}
The tables are:
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',
`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 code below. I am getting a collection could not be initialised exception.
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());
Please let me know if there is anything wrong in the code. Help appreciated !
Upvotes: 4
Views: 318
Reputation: 845
Sorry to be late in my response. Thanks for all your responses. This is the way I have made it work DeviceWithReading.java has
private Set<DeviceLatestData> deviceReadings = new HashSet<DeviceLatestData>();
@OneToMany(mappedBy= "device", fetch = FetchType.EAGER)
@Cascade(CascadeType.SAVE_UPDATE)
public Set<DeviceLatestData> getDeviceReadings()
{
return deviceReadings;
}
DeviceLatestData has
private DeviceWithReading device;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "device_id" , nullable = false)
public DeviceWithReading getDevice()
{
return device;
}
The code for listing is
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();
Could not integrate the resulttransformer for VO generation, so iterated and set data in VO on my own. There was an issue with some fields not being there in the table but used in my BaseMasterEntity.java class (created by modified by etc) I resolved that as well by changing the table definition for DeviceLatestData.
Thanks again
Upvotes: 0
Reputation: 1097
You must declare the @Id fields in your entities in order for the ManyToOne association to work:
@Id
@Column(name="id")
@GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
Upvotes: 3