Euhm... guess my problem is not as trivial as it looked... Thanks for helping. To allow you fully to understand my problem, I am sending the sql script for my database. First of all, it's SNORT database and the design is not so good (legacy I suspect). As you can all understand, I cannot change the database or in very slight ways, so as not to break SNORT. Also, another major problem, foreign keys are implicit (not implemented in the database schema but only in the application...)
Code:
-- MySQL Administrator dump 1.4
--
-- ------------------------------------------------------
-- Server version 4.1.12a-nt
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
--
-- Create schema snort
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ snort;
USE snort;
--
-- Table structure for table `snort`.`data`
--
DROP TABLE IF EXISTS `data`;
CREATE TABLE `data` (
`sid` int(10) unsigned NOT NULL default '0',
`cid` int(10) unsigned NOT NULL default '0',
`data_payload` text,
PRIMARY KEY (`sid`,`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`data`
--
/*!40000 ALTER TABLE `data` DISABLE KEYS */;
/*!40000 ALTER TABLE `data` ENABLE KEYS */;
--
-- Table structure for table `snort`.`detail`
--
DROP TABLE IF EXISTS `detail`;
CREATE TABLE `detail` (
`detail_type` tinyint(4) unsigned NOT NULL default '0',
`detail_text` text NOT NULL,
PRIMARY KEY (`detail_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`detail`
--
/*!40000 ALTER TABLE `detail` DISABLE KEYS */;
INSERT INTO `detail` (`detail_type`,`detail_text`) VALUES
(0,'fast'),
(1,'full');
/*!40000 ALTER TABLE `detail` ENABLE KEYS */;
--
-- Table structure for table `snort`.`encoding`
--
DROP TABLE IF EXISTS `encoding`;
CREATE TABLE `encoding` (
`encoding_type` tinyint(4) unsigned NOT NULL default '0',
`encoding_text` text NOT NULL,
PRIMARY KEY (`encoding_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`encoding`
--
/*!40000 ALTER TABLE `encoding` DISABLE KEYS */;
/*!40000 ALTER TABLE `encoding` ENABLE KEYS */;
--
-- Table structure for table `snort`.`event`
--
DROP TABLE IF EXISTS `event`;
CREATE TABLE `event` (
`sid` int(10) unsigned NOT NULL default '0',
`cid` int(10) unsigned NOT NULL default '0',
`signature` int(10) unsigned NOT NULL default '0',
`timestamp` varchar(27) NOT NULL default '0',
PRIMARY KEY (`sid`,`cid`),
KEY `sig` (`signature`),
KEY `time` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`event`
--
/*!40000 ALTER TABLE `event` DISABLE KEYS */;
/*!40000 ALTER TABLE `event` ENABLE KEYS */;
--
-- Table structure for table `snort`.`icmphdr`
--
DROP TABLE IF EXISTS `icmphdr`;
CREATE TABLE `icmphdr` (
`sid` int(10) unsigned NOT NULL default '0',
`cid` int(10) unsigned NOT NULL default '0',
`icmp_type` tinyint(3) unsigned NOT NULL default '0',
`icmp_code` tinyint(3) unsigned NOT NULL default '0',
`icmp_csum` smallint(5) unsigned default NULL,
`icmp_id` smallint(5) unsigned default NULL,
`icmp_seq` smallint(5) unsigned default NULL,
PRIMARY KEY (`sid`,`cid`),
KEY `icmp_type` (`icmp_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`icmphdr`
--
/*!40000 ALTER TABLE `icmphdr` DISABLE KEYS */;
/*!40000 ALTER TABLE `icmphdr` ENABLE KEYS */;
--
-- Table structure for table `snort`.`iphdr`
--
DROP TABLE IF EXISTS `iphdr`;
CREATE TABLE `iphdr` (
`sid` int(10) unsigned NOT NULL default '0',
`cid` int(10) unsigned NOT NULL default '0',
`ip_src` int(10) unsigned NOT NULL default '0',
`ip_dst` int(10) unsigned NOT NULL default '0',
`ip_ver` tinyint(3) unsigned default NULL,
`ip_hlen` tinyint(3) unsigned default NULL,
`ip_tos` tinyint(3) unsigned default NULL,
`ip_len` smallint(5) unsigned default NULL,
`ip_id` smallint(5) unsigned default NULL,
`ip_flags` tinyint(3) unsigned default NULL,
`ip_off` smallint(5) unsigned default NULL,
`ip_ttl` tinyint(3) unsigned default NULL,
`ip_proto` tinyint(3) unsigned NOT NULL default '0',
`ip_csum` smallint(5) unsigned default NULL,
PRIMARY KEY (`sid`,`cid`),
KEY `ip_src` (`ip_src`),
KEY `ip_dst` (`ip_dst`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`iphdr`
--
/*!40000 ALTER TABLE `iphdr` DISABLE KEYS */;
/*!40000 ALTER TABLE `iphdr` ENABLE KEYS */;
--
-- Table structure for table `snort`.`opt`
--
DROP TABLE IF EXISTS `opt`;
CREATE TABLE `opt` (
`sid` int(10) unsigned NOT NULL default '0',
`cid` int(10) unsigned NOT NULL default '0',
`optid` int(10) unsigned NOT NULL default '0',
`opt_proto` tinyint(3) unsigned NOT NULL default '0',
`opt_code` tinyint(3) unsigned NOT NULL default '0',
`opt_len` smallint(6) default NULL,
`opt_data` text,
PRIMARY KEY (`sid`,`cid`,`optid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`opt`
--
/*!40000 ALTER TABLE `opt` DISABLE KEYS */;
/*!40000 ALTER TABLE `opt` ENABLE KEYS */;
--
-- Table structure for table `snort`.`reference`
--
DROP TABLE IF EXISTS `reference`;
CREATE TABLE `reference` (
`ref_id` int(10) unsigned NOT NULL auto_increment,
`ref_system_id` int(10) unsigned NOT NULL default '0',
`ref_tag` text NOT NULL,
PRIMARY KEY (`ref_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`reference`
--
/*!40000 ALTER TABLE `reference` DISABLE KEYS */;
/*!40000 ALTER TABLE `reference` ENABLE KEYS */;
--
-- Table structure for table `snort`.`reference_system`
--
DROP TABLE IF EXISTS `reference_system`;
CREATE TABLE `reference_system` (
`ref_system_id` int(10) unsigned NOT NULL auto_increment,
`ref_system_name` varchar(20) default NULL,
PRIMARY KEY (`ref_system_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`reference_system`
--
/*!40000 ALTER TABLE `reference_system` DISABLE KEYS */;
/*!40000 ALTER TABLE `reference_system` ENABLE KEYS */;
--
-- Table structure for table `snort`.`schema`
--
DROP TABLE IF EXISTS `schema`;
CREATE TABLE `schema` (
`vseq` int(10) unsigned NOT NULL default '0',
`ctime` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`vseq`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`schema`
--
/*!40000 ALTER TABLE `schema` DISABLE KEYS */;
INSERT INTO `schema` (`vseq`,`ctime`) VALUES
(106,'2005-07-15 16:06:56');
/*!40000 ALTER TABLE `schema` ENABLE KEYS */;
--
-- Table structure for table `snort`.`sensor`
--
DROP TABLE IF EXISTS `sensor`;
CREATE TABLE `sensor` (
`sid` int(10) unsigned NOT NULL auto_increment,
`hostname` text,
`interface` text,
`filter` text,
`detail` tinyint(4) default NULL,
`encoding` tinyint(4) default NULL,
`last_cid` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`sensor`
--
/*!40000 ALTER TABLE `sensor` DISABLE KEYS */;
/*!40000 ALTER TABLE `sensor` ENABLE KEYS */;
--
-- Table structure for table `snort`.`sig_class`
--
DROP TABLE IF EXISTS `sig_class`;
CREATE TABLE `sig_class` (
`sig_class_id` int(10) unsigned NOT NULL auto_increment,
`sig_class_name` varchar(60) NOT NULL default '',
PRIMARY KEY (`sig_class_id`),
KEY `sig_class_id` (`sig_class_id`),
KEY `sig_class_name` (`sig_class_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`sig_class`
--
/*!40000 ALTER TABLE `sig_class` DISABLE KEYS */;
/*!40000 ALTER TABLE `sig_class` ENABLE KEYS */;
--
-- Table structure for table `snort`.`sig_reference`
--
DROP TABLE IF EXISTS `sig_reference`;
CREATE TABLE `sig_reference` (
`sig_id` int(10) unsigned NOT NULL default '0',
`ref_seq` int(10) unsigned NOT NULL default '0',
`ref_id` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`sig_id`,`ref_seq`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`sig_reference`
--
/*!40000 ALTER TABLE `sig_reference` DISABLE KEYS */;
/*!40000 ALTER TABLE `sig_reference` ENABLE KEYS */;
--
-- Table structure for table `snort`.`signature`
--
DROP TABLE IF EXISTS `signature`;
CREATE TABLE `signature` (
`sig_id` int(10) unsigned NOT NULL auto_increment,
`sig_name` varchar(255) NOT NULL default '',
`sig_class_id` int(10) unsigned NOT NULL default '0',
`sig_priority` int(10) unsigned default NULL,
`sig_rev` int(10) unsigned default NULL,
`sig_sid` int(10) unsigned default NULL,
PRIMARY KEY (`sig_id`),
KEY `sign_idx` (`sig_name`(20)),
KEY `sig_class_id_idx` (`sig_class_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`signature`
--
/*!40000 ALTER TABLE `signature` DISABLE KEYS */;
/*!40000 ALTER TABLE `signature` ENABLE KEYS */;
--
-- Table structure for table `snort`.`tcphdr`
--
DROP TABLE IF EXISTS `tcphdr`;
CREATE TABLE `tcphdr` (
`sid` int(10) unsigned NOT NULL default '0',
`cid` int(10) unsigned NOT NULL default '0',
`tcp_sport` smallint(5) unsigned NOT NULL default '0',
`tcp_dport` smallint(5) unsigned NOT NULL default '0',
`tcp_seq` int(10) unsigned default NULL,
`tcp_ack` int(10) unsigned default NULL,
`tcp_off` tinyint(3) unsigned default NULL,
`tcp_res` tinyint(3) unsigned default NULL,
`tcp_flags` tinyint(3) unsigned NOT NULL default '0',
`tcp_win` smallint(5) unsigned default NULL,
`tcp_csum` smallint(5) unsigned default NULL,
`tcp_urp` smallint(5) unsigned default NULL,
PRIMARY KEY (`sid`,`cid`),
KEY `tcp_sport` (`tcp_sport`),
KEY `tcp_dport` (`tcp_dport`),
KEY `tcp_flags` (`tcp_flags`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`tcphdr`
--
/*!40000 ALTER TABLE `tcphdr` DISABLE KEYS */;
/*!40000 ALTER TABLE `tcphdr` ENABLE KEYS */;
--
-- Table structure for table `snort`.`udphdr`
--
DROP TABLE IF EXISTS `udphdr`;
CREATE TABLE `udphdr` (
`sid` int(10) unsigned NOT NULL default '0',
`cid` int(10) unsigned NOT NULL default '0',
`udp_sport` smallint(5) unsigned NOT NULL default '0',
`udp_dport` smallint(5) unsigned NOT NULL default '0',
`udp_len` smallint(5) unsigned default NULL,
`udp_csum` smallint(5) unsigned default NULL,
PRIMARY KEY (`sid`,`cid`),
KEY `udp_sport` (`udp_sport`),
KEY `udp_dport` (`udp_dport`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `snort`.`udphdr`
--
/*!40000 ALTER TABLE `udphdr` DISABLE KEYS */;
/*!40000 ALTER TABLE `udphdr` ENABLE KEYS */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
Here's my mapping file for table Event
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.2
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="mysqlDAO.Event"
table="event"
>
<meta attribute="implement-equals" inherit="false">true</meta>
<composite-id name="comp_id" class="mysqlDAO.EventPK">
<key-property
name="sid"
column="sid"
type="java.lang.Integer"
length="10"
/>
<key-property
name="cid"
column="cid"
type="java.lang.Integer"
length="10"
/>
</composite-id>
<property
name="timestamp"
type="java.lang.String"
column="timestamp"
not-null="true"
length="27"
/>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- bi-directional many-to-one association to Sensor -->
<many-to-one
name="sensor"
class="mysqlDAO.Sensor"
update="false"
insert="false"
>
<column name="sid" />
</many-to-one>
<!-- end of derived association(s) -->
<!-- bi-directional many-to-one association to Signature -->
<many-to-one
name="signature"
class="mysqlDAO.Signature"
not-null="true"
>
<column name="signature" />
</many-to-one>
<!-- bi-directional one-to-one association to Iphdr -->
<one-to-one
name="iphdr"
class="mysqlDAO.Iphdr"
outer-join="auto"
/>
</class>
</hibernate-mapping>
Here's Event.java
Code:
package mysqlDAO;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.text.ParseException;
/** @author Hibernate CodeGenerator */
public class Event implements Serializable {
/** identifier field */
private mysqlDAO.EventPK comp_id;
/** persistent field */
private String timestamp;
/** nullable persistent field */
private mysqlDAO.Iphdr iphdr;
/** nullable persistent field */
private mysqlDAO.Sensor sensor;
/** persistent field */
private mysqlDAO.Signature signature;
/** Variable servant à la présentation du format de la date*/
private SimpleDateFormat df;
/**
* Snort, avec le paramètre -U enregistre le temps au format UTC à GMT+0000
* Il faut en tenir compte quand on entre des valeurs de temps.
*
*/
private void initDateFormat()
{
String dateFormatPattern = "yyyy-MM-dd H:mm:ss.SSS";
TimeZone greenwitch = TimeZone.getTimeZone("GMT");
df = new SimpleDateFormat(dateFormatPattern);
df.setTimeZone(greenwitch);
}
/** full constructor */
public Event(mysqlDAO.EventPK comp_id, Date timestamp, mysqlDAO.Iphdr iphdr, mysqlDAO.Sensor sensor, mysqlDAO.Signature signature) {
initDateFormat();
this.comp_id = comp_id;
this.timestamp = df.format(timestamp);
this.iphdr = iphdr;
this.sensor = sensor;
this.signature = signature;
}
/** default constructor */
public Event() {
initDateFormat();
}
/** minimal constructor */
public Event(mysqlDAO.EventPK comp_id, Date timestamp, mysqlDAO.Signature signature) {
initDateFormat();
this.comp_id = comp_id;
this.timestamp = df.format(timestamp);
this.signature = signature;
}
public mysqlDAO.EventPK getComp_id() {
return this.comp_id;
}
public void setComp_id(mysqlDAO.EventPK comp_id) {
this.comp_id = comp_id;
}
public Date getTimestamp() throws ParseException
{
Date eventDateTime = df.parse(this.timestamp);
return eventDateTime;
}
public void setTimestamp(Date timestamp) {
this.timestamp = df.format(timestamp);
}
public mysqlDAO.Iphdr getIphdr() {
return this.iphdr;
}
public void setIphdr(mysqlDAO.Iphdr iphdr) {
this.iphdr = iphdr;
}
public mysqlDAO.Sensor getSensor() {
return this.sensor;
}
public void setSensor(mysqlDAO.Sensor sensor) {
this.sensor = sensor;
}
public mysqlDAO.Signature getSignature() {
return this.signature;
}
public void setSignature(mysqlDAO.Signature signature) {
this.signature = signature;
}
public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( !(other instanceof Event) ) return false;
Event castOther = (Event) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}
}
And finally here's EventPK.java
Code:
package mysqlDAO;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class EventPK implements Serializable {
/** identifier field */
private Integer sid;
/** identifier field */
private Integer cid;
/** full constructor */
public EventPK(Integer sid, Integer cid) {
this.sid = sid;
this.cid = cid;
}
/** default constructor */
public EventPK() {
}
public Integer getSid() {
return this.sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public Integer getCid() {
return this.cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String toString() {
return new ToStringBuilder(this)
.append("sid", getSid())
.append("cid", getCid())
.toString();
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( !(other instanceof EventPK) ) return false;
EventPK castOther = (EventPK) other;
return new EqualsBuilder()
.append(this.getSid(), castOther.getSid())
.append(this.getCid(), castOther.getCid())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getSid())
.append(getCid())
.toHashCode();
}
}
Also here's my test code
Code:
public void TestQueryEvent()
{
try
{
/** Select the last entry from table Event*/
tx = session.beginTransaction();
Query q = session.createQuery("from Event");
List queryResult = q.list();
System.out.println(queryResult.size());
Iterator qResultIter = queryResult.iterator();
Event lastEvent;
while(qResultIter.hasNext())
{
lastEvent = (Event)qResultIter.next();
System.out.println(lastEvent.toString());
}
tx.commit();
assertTrue(tx.wasCommitted());
}
catch(HibernateException e)
{
log.warn(this.getName()+" -- Unable to execute query");
}
}
protected void setUp() throws Exception {
super.setUp();
log = Logger.getLogger("log4j.logger.mysqlDAO");
session = HibernateUtil.getSession();
}
protected void tearDown() throws Exception {
super.tearDown();
HibernateUtil.closeSession();
}
The Question is simple... what's wrong with my code and why do I get an error when I try to make a query...
Thanks to you all,
Simon