Hello
[b]Hibernate version:3.0---Annotation version:3.0 ----Database-Postgresql
[b]My Database Tables are:--
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | devices | table | postgres
public | event | table | postgres
public | host | table | postgres
Table "public.devices"
Column | Type | Modifiers
----------+---------+-----------
deviceid | integer | not null
name | text |
Indexes:
"devices_pkey" PRIMARY KEY, btree (deviceid)
------------------------------------------------------------------------------------------------
Table "public.event"
Column | Type | Modifiers
----------+---------+-----------
event_id | integer | not null
sid | integer | not null
cid | integer | not null
Indexes:
"event_pkey" PRIMARY KEY, btree (sid, cid)
------------------------------------------------------------------------------------------------
Table "public.host"
Column | Type | Modifiers
-----------+---------+-----------
host_id | integer | not null
device_id | integer |
event_sid | integer |
event_cid | integer |
Indexes:
"host_pkey" PRIMARY KEY, btree (host_id)
Foreign-key constraints:
"host_event_sid_fkey" FOREIGN KEY (event_sid, event_cid) REFERENCES event(sid, cid)
---------------------------------------------------------------------------------------------------
Mapping Classes are:------DEVICES
package model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="devices")
public class Devices
{
@Id
@Column(name="deviceid")
Integer deviceid;
@Column(name="name")
String name;
public Integer getDeviceid() {
return deviceid;
}
public void setDeviceid(Integer deviceid) {
this.deviceid = deviceid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
---------------------------------------------------------------------------------------------------------
EVENT
package model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="event")
public class Event
{
@Id
@Column(name="event_id")
Integer event_id;
@ManyToOne
@JoinColumn(name = "sid", insertable = false, updatable = false, nullable = false)
private Devices device;
@Column(insertable = false, updatable = false, nullable = false)
Integer sid;
@Column(insertable = false, updatable = false)
Integer cid; // eventID from Snort
public Integer getEvent_id() {
return event_id;
}
public void setEvent_id(Integer event_id) {
this.event_id = event_id;
}
public Devices getDevice() {
return device;
}
public void setDevice(Devices device) {
this.device = device;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
}
-------------------------------------------------------------------------------------------------------------
HOST
package model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="host")
public class Host
{
@Id
@Column(name="host_id")
Integer host_id;
@ManyToOne
@JoinColumn(name = "device_id", nullable = false)
Devices device;
@ManyToOne()
@JoinColumns
({
@JoinColumn(name = "event_sid", referencedColumnName = "sid"),
@JoinColumn(name = "event_cid", referencedColumnName = "cid")
})
Event compromiseEvent;
public Integer getHost_id() {
return host_id;
}
public void setHost_id(Integer host_id) {
this.host_id = host_id;
}
public Devices getDevice() {
return device;
}
public void setDevice(Devices device) {
this.device = device;
}
public Event getCompromiseEvent() {
return compromiseEvent;
}
public void setCompromiseEvent(Event compromiseEvent) {
this.compromiseEvent = compromiseEvent;
}
}
----------------------------------------------------------------------------------------------------------
In Main Method:-----
public static Collection getAllHost()
{
Criteria crit=null;
try
{
Session session=getThreadedSession();
Transaction tr=session.beginTransaction();
crit=session.createCriteria(Host.class);
}
catch(Exception ex)
{
System.out.println("Exception Generated"+ex);
}
return crit.list();
}
And The Error is :----
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select this_.host_id as host1_2_3_, this_.event_sid as event2_2_3_, this_.event_cid as event3_2_3_, this_.device_id as device4_2_3_, event2_.event_id as event1_1_0_, event2_.cid as cid1_0_, event2_.sid as sid1_0_, devices3_.deviceid as deviceid0_1_, devices3_.name as name0_1_, devices4_.deviceid as deviceid0_2_, devices4_.name as name0_2_ from host this_ left outer join event event2_ on this_.event_sid=event2_.sid and this_.event_cid=event2_.cid left outer join devices devices3_ on event2_.sid=devices3_.deviceid inner join devices devices4_ on this_.device_id=devices4_.deviceid
Hibernate: select devices0_.deviceid as deviceid0_0_, devices0_.name as name0_0_ from devices devices0_ where devices0_.deviceid=?
Exception Generated in MAIN---java.lang.ClassCastException: model.Event
Kind Regards
Khirod Patra
|