-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Query problem - Composite key
PostPosted: Wed Jul 27, 2005 5:48 pm 
Newbie

Joined: Sat Jul 16, 2005 2:57 pm
Posts: 7
Location: Montréal, Canada
I have a major problem that I need your help on:

Here's one of my table of my database

Table Event
--------------
sid
cid
signature
timestamp

PK (sid, cid)
FK1 (signature)
FK2 (sid)

I used middlegen to reverse engineer the database to POJO and ended up with 2 classes (Event.java and EventPK.java)

I'm trying to query the table Event and retrieve every object from the table Event with the following HQL statement:

...
public void TestQueryEvent()
{
try
{
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");
}
}
...

Turns out that this query always fails!

It seems hibernate tries the following sql:

Hibernate: select event0_.sid as sid, event0_.cid as cid, event0_.timestamp as timestamp3_, event0_.sid as sid3_, event0_.signature as signature3_ from event event0_

I would really like to retrieve a full Event object and would really like to know how (and why that thing doesn't work...)?

I think the problem has to do with the composite key...


Thanks,

Simon

_________________
École Polytechnique de Montréal


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 6:35 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
what is your mapping ?


Top
 Profile  
 
 Post subject: Here's my mapping
PostPosted: Thu Jul 28, 2005 10:42 am 
Newbie

Joined: Sat Jul 16, 2005 2:57 pm
Posts: 7
Location: Montréal, Canada
Here's my mapping between table Event and classes Event.java and EventPK.java

<?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>


Thanks for any help you can give me,

Simon

_________________
École Polytechnique de Montréal


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 10:57 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
What about Event.java ? what is error ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 11:04 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Try composite id like this :

<composite-id name="comp_id" class="mysqlDAO.EventPK">
<key-many-to-one
name="sensor"
column="sid"
length="10"
/>
<key-property
name="cid"
column="cid"
type="java.lang.Integer"
length="10"
/>
</composite-id>

and change EventPK property sid to
Sensor sensor

regards


Top
 Profile  
 
 Post subject: Sorry, it did not work
PostPosted: Thu Jul 28, 2005 11:50 am 
Newbie

Joined: Sat Jul 16, 2005 2:57 pm
Posts: 7
Location: Montréal, Canada
I modified my file Event.hbm.xml with your proposed modifications and modified the file EventPK.java with Sensor sensor (is that what you meant by changing the property of EventPK ?)

Turns out trying to query "from Event" still fails...


Thanks for trying... :.)

If you have other ideas, let me know!


Simon

_________________
École Polytechnique de Montréal


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 1:32 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
send us Event.java,EventPK.java and error log for your session


Top
 Profile  
 
 Post subject: My guess
PostPosted: Thu Jul 28, 2005 6:59 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
If I understand your question correctly then your DB schema looks like this:
CREATE TABLE signature (
cid int PRIMARY KEY,
name varchar(20)
);

CREATE TABLE sensor (
sid int PRIMARY KEY,
name varchar(20)
);

CREATE TABLE event (
sid int ,
cid int,
signature varchar(20),
"timestamp" varchar(20),
PRIMARY KEY (sid,cid)
);

ALTER TABLE event ADD FOREIGN KEY (cid) references signature(cid);
ALTER TABLE event ADD FOREIGN KEY (sid) references sensor(sid);



INSERT INTO sensor VALUES (10, 'sensor 10');
INSERT INTO signature VALUES ( 20, 'signature 20');

INSERT INTO event VALUES ( 10, 20, 'sign1', 'ts1');

Then the following mapping works for ( Hibernate 3.1-cvs)

<?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 package="ft945595">


<class
name="Event"
table="event"
>
<meta attribute="implement-equals" inherit="false">true</meta>

<composite-id name="id" class="EventPK">
<key-many-to-one
name="sid"
column="sid"
entity-name="ft945595.Sensor"
/>
<key-many-to-one
name="cid"
column="cid"
entity-name="ft945595.Signature"
/>
</composite-id>

<property
name="timestamp"
type="java.lang.String"
column="timestamp"
not-null="true"
length="27"
/>



</class>

<class name="Sensor" table="sensor">
<id column="sid" type="java.lang.Integer" unsaved-value="null">
<generator class="assigned" />
</id>
<property name="name" />
</class>


<class name="Signature" table="sensor">
<id column="cid" type="java.lang.Integer" unsaved-value="null">
<generator class="assigned" />
</id>
<property name="name" />
</class>

</hibernate-mapping>

All the sources in the attachment


Top
 Profile  
 
 Post subject: Oops!
PostPosted: Thu Jul 28, 2005 7:03 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Oops, there is no way to attach a file :(
Code:
public class Event {
  EventPK id;
  String  timestamp = String.valueOf( System.currentTimeMillis());

  public EventPK getId() {
    return id;
  }

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

  public String getTimestamp() {
    return timestamp;
  }

  public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
  }
}




Code:
public class EventPK implements Serializable {
  Sensor sid;
  Signature cid;

  public Sensor getSid() {
    return sid;
  }

  public void setSid(Sensor sid) {
    this.sid = sid;
  }

  public Signature getCid() {
    return cid;
  }

  public void setCid(Signature cid) {
    this.cid = cid;
  }

  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    final EventPK eventPK = (EventPK) o;

    if (cid != null ? !cid.equals(eventPK.cid) : eventPK.cid != null) return false;
    if (sid != null ? !sid.equals(eventPK.sid) : eventPK.sid != null) return false;

    return true;
  }

  public int hashCode() {
    int result;
    result = (sid != null ? sid.hashCode() : 0);
    result = 29 * result + (cid != null ? cid.hashCode() : 0);
    return result;
  }
}


Signature and Sensor are trivial.



Hope that helps.


Top
 Profile  
 
 Post subject: The Holy Code... so we can see the light... I hope ;.)
PostPosted: Fri Jul 29, 2005 2:16 pm 
Newbie

Joined: Sat Jul 16, 2005 2:57 pm
Posts: 7
Location: Montréal, Canada
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

_________________
École Polytechnique de Montréal


Top
 Profile  
 
 Post subject: About foreign keys
PostPosted: Fri Jul 29, 2005 2:21 pm 
Newbie

Joined: Sat Jul 16, 2005 2:57 pm
Posts: 7
Location: Montréal, Canada
Ok, before anyone asks, the database doesn't implement foreign keys and I can't add them because it breaks SNORT, relationship have to be implemented using the mapping file of middlegen so here it is:

Code:

<?xml version="1.0" encoding="utf-8"?>

<!--
This build file is generated by MiddlegenIDE.

MiddlegenIDE: http://ultimania.org/middlegenide/
-->

<project name="Middlegen Hibernate" default="compile" basedir="../.">
   <property file=".././build.properties"/>
   <property name="hibernate.cascade" value="all"/>
   <property name="package" value="mysqlDAO"/>
   <property name="gen.xdoclet-tag" value="false"/>
   <property name="gui" value="true"/>
   <property name="jdbc.jar"
      value="C:\INTRUSION\libraries\mysql-connector-java-3.1.10\mysql-connector-java-3.1.10-bin.jar"/>
   <property name="database.driver" value="com.mysql.jdbc.Driver"/>
   <property name="lib.dir"
      value="/c:/INTRUSION/eclipse/plugins/net.sf.middlegen_2.1.2/lib/"/>
   <property name="database.url" value="jdbc:mysql://localhost:3306/snort"/>
   <property name="database.userid" value="snort"/>
   <property name="database.password" value="iamnotgoingtotellyou"/>
   <property name="database.schema" value=""/>
   <property name="database.catalog" value=""/>
   <property name="dest.dir" value="src"/>
   
   <target name="init"
      depends="prepare,fail-if-no-middlegen,fail-if-no-hibernate,fail-if-no-hibernate-ext">
      
      <taskdef name="middlegen" classname="middlegen.MiddlegenTask"
         classpathref="middlegen.classpath"/>
      
      <taskdef name="hbm2java"
         classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
         classpathref="hibernate-ext.classpath"/>
      
      <mkdir dir="${dest.dir}"/>
      
   </target>
   
   <target name="prepare">
      
      <path id="middlegen.classpath">
         <pathelement path="${jdbc.jar}"/>
         <fileset dir="${lib.dir}" includes="*.jar"/>
      </path>
      
      <path id="hibernate-ext.classpath">
         <fileset dir="${lib.dir}" includes="*.jar"/>
      </path>
      
      <available property="middlegen" classname="middlegen.MiddlegenTask"
         classpathref="middlegen.classpath"/>
      <available property="hibernate" classname="net.sf.hibernate.Hibernate"
         classpathref="hibernate-ext.classpath"/>
      <available property="hibernate-ext"
         classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
         classpathref="hibernate-ext.classpath"/>
      
   </target>
   
   <target name="fail-if-no-middlegen" unless="middlegen">
      <fail> Middlegen is not found. Please install Middlegen.
      </fail>
   </target>
   
   <target name="fail-if-no-hibernate" unless="hibernate">
      <fail> Hibernate is not found. Please install Hibernate.
      </fail>
   </target>
   
   <target name="fail-if-no-hibernate-ext" unless="hibernate-ext">
      <fail> Hibernate-Extension is not found. Please install Hibernate-Extenstion.
      </fail>
   </target>
   
   <target name="gen-hbm" depends="init">
      
      <middlegen appname="mysqlDAO" prefsdir="." gui="${gui}"
         databaseurl="${database.url}" driver="${database.driver}"
         username="${database.userid}" password="${database.password}"
         schema="${database.schema}" catalog="${database.catalog}">
         <hibernate version="3.0" destination="${dest.dir}" package="${package}"
            genXDocletTags="${gen.xdoclet-tag}"
            standardCascade="${hibernate.cascade}"
            javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"/>
         
         <table name="data"/>
         <table name="detail">
            <crossref fktable="sensor" fkcolumn="detail"
               pkcolumn="detail_type"/>
         </table>
         <table name="encoding">
            <crossref fktable="sensor" fkcolumn="encoding"
               pkcolumn="encoding_type"/>
         </table>
         <table name="event">
            <crossref fktable="iphdr" fkcolumn="cid" pkcolumn="cid" name="id"/>
            <crossref fktable="iphdr" fkcolumn="sid" pkcolumn="sid" name="id"/>
         </table>
         <table name="icmphdr"/>
         <table name="iphdr">
            <crossref fktable="data" fkcolumn="cid" pkcolumn="cid" name="id"/>
            <crossref fktable="data" fkcolumn="sid" pkcolumn="sid" name="id"/>
            <crossref fktable="opt" fkcolumn="cid" pkcolumn="cid" name="id2"/>
            <crossref fktable="opt" fkcolumn="sid" pkcolumn="sid" name="id2"/>
            <crossref fktable="tcphdr" fkcolumn="cid" pkcolumn="cid" name="id3"/>
            <crossref fktable="tcphdr" fkcolumn="sid" pkcolumn="sid" name="id3"/>
            <crossref fktable="udphdr" fkcolumn="cid" pkcolumn="cid" name="id4"/>
            <crossref fktable="udphdr" fkcolumn="sid" pkcolumn="sid" name="id4"/>
            <crossref fktable="icmphdr" fkcolumn="cid" pkcolumn="cid" name="id5"/>
            <crossref fktable="icmphdr" fkcolumn="sid" pkcolumn="sid" name="id5"/>
         </table>
         <table name="opt"/>
         <table name="reference">
            <crossref fktable="sig_reference" fkcolumn="ref_id"   pkcolumn="ref_id"/>
         </table>
         <table name="reference_system">
            <crossref fktable="reference" fkcolumn="ref_system_id" pkcolumn="ref_system_id"/>
         </table>
         <table name="schema"/>
         <table name="sensor">
            <crossref fktable="event" fkcolumn="sid" pkcolumn="sid"/>
         </table>
         <table name="sig_class">
            <crossref fktable="signature" fkcolumn="sig_class_id" pkcolumn="sig_class_id"/>
         </table>
         <table name="sig_reference"/>
         <table name="signature">
            <crossref fktable="sig_reference" fkcolumn="sig_id" pkcolumn="sig_id"/>
            <crossref fktable="event" fkcolumn="signature" pkcolumn="sig_id"/>
         </table>
         <table name="tcphdr"/>
         <table name="udphdr"/>
      </middlegen>
   </target>
   
   <target name="gen-java" depends="gen-hbm">
      <hbm2java output="${dest.dir}">
         <fileset dir="${dest.dir}">
            <include name="**/*.hbm.xml"/>
         </fileset>
      </hbm2java>
   </target>
   
   <target name="compile" depends="gen-java">
   </target>
   
</project>


Thanks,

Simon

_________________
École Polytechnique de Montréal


Top
 Profile  
 
 Post subject: Stack trace?
PostPosted: Wed Aug 03, 2005 11:00 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Why do not you post the stack trace?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.