Hibernate version:2.1
Mapping documents:needed
Code between sessionFactory.openSession() and session.close():n/a
Full stack trace of any exception that occurs:n/a
Name and version of the database you are using:Pointbase
Debug level Hibernate log excerpt:n/a
Can some kind sole please tell me how to create the two mapping files for this simple schema? I have been trying for many days now and nothing is working how I want it to. This is the schema that was generated, except for the additional column name on the foreign key constraint that I added here because I don't know how to tell Hibernate to specify the column name of the parent table. Besides that, it seems right, but the test code to simply fetch the records is failing. I need mapping files. I know it is my mapping files that are wrong. I know I'm stupid. Laugh first, then help, please. I've got to make some progress soon.
create table weblogic.BANDS (
NAME VARCHAR(150) not null,
STARTDATE DATE,
FOUNDER VARCHAR(150),
WLS_TEMP INTEGER,
primary key (NAME)
);
create table weblogic.RECORDING (
BANDNAME VARCHAR(150) not null,
TITLE VARCHAR(150) not null,
RATING VARCHAR(150),
BANDEJB_NAME VARCHAR(150),
WLS_TEMP INTEGER,
primary key (BANDNAME, TITLE)
);
alter table weblogic.RECORDING add constraint FKE1508DD1BF7F4E40 foreign key (BANDNAME) references weblogic.BANDS (NAME);
Here's what I wish I could make work, but can't:
1) I want the BANDS class to have a bag of RECORDINGs, that is NOT bidirectional.
2) I want the one-to-many from BANDS.NAME to RECORDINGS.BANDNAME.
3) I want to be able to query for either bands (and get recordings too) or recordings by band name and all recordings.
4) I want to be able to generate the composite id as an inner class using hbm2java.
So far, I have been able to get little pieces of this combination to work, but nothing substantial. It looks like it should be so easy!
This is what I have:
BANDBEAN mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping schema="weblogic" package="relationships">
<class name="BandBean" table="BANDS" lazy="true">
<meta attribute="class-description">
This JavaBean class is for interacting with the BANDS
table.
@author Me
</meta>
<id name="name" column="NAME" type="string" length="150" unsaved-value="null" >
<generator class="assigned"/>
</id>
<property name="startdate" type="date"
column="STARTDATE" length="10" not-null="false">
<meta attribute="field-description">Returns band formation date.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="founder" type="string"
column="FOUNDER" length="150" not-null="false">
<meta attribute="field-description">Returns band founder name.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="wls_temp" type="integer"
column="WLS_TEMP" length="10" not-null="false">
<meta attribute="field-description">Returns WebLogic-specific data.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<bag name="recordings" lazy="true" inverse="false" order-by="BANDNAME">
<meta attribute="field-description">List of Recordings this band has made.</meta>
<meta attribute="use-in-tostring">true</meta>
<key column="BANDNAME"/>
<one-to-many class="RecordingBean"/>
</bag>
</class>
<!-- named queries -->
<query name="relationships.allBands"><![CDATA[
from relationships.BandBean as b order by b.name
]]></query>
</hibernate-mapping>
RECORDINGBEAN mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping schema="weblogic" package="relationships">
<class name="RecordingBean" table="RECORDING" lazy="true">
<meta attribute="class-description">
This JavaBean class is for interacting with the RECORDING
table.
@author Me
</meta>
<composite-id>
<key-property name="bandname" column="BANDNAME" type="string" length="150">
<meta attribute="field-description">Returns band's name.</meta>
<meta attribute="use-in-tostring">true</meta>
</key-property>
<key-property name="title" column="TITLE" type="string" length="150">
<meta attribute="field-description">Returns recording name.</meta>
<meta attribute="use-in-tostring">true</meta>
</key-property>
</composite-id>
<property name="rating" column="RATING"
type="string" length="150" not-null="false">
<meta attribute="field-description">Returns popularity rating for the recording.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="bandejb_name" column="BANDEJB_NAME"
type="string" length="150" not-null="false">
<meta attribute="field-description">Returns WebLogic-specific data.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="wls_temp" column="WLS_TEMP"
type="integer" length="10" not-null="false">
<meta attribute="field-description">Returns WebLogic-specific data.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
<!-- commented out because I don't want RecordingBean to have a
reference to BandBean...
<many-to-one name="BandBean" class="BandBean" column="NAME" not-null="true"/>
-->
</class>
<!-- named queries -->
<query name="relationships.recordingsByBand"><![CDATA[
from relationships.RecordingBean as rb
where upper(rb.bandname) = :name
order by rb.bandname, rb.title
]]></query>
</hibernate-mapping>
The Test BandBean Code:
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
protected void setUp() throws MappingException, HibernateException {
ayNames_ = new String[] {"Eagles", "Aerosmith", "Nirvana"};
ayFounders_= new String[] {"Glen Frey", "Steven Tyler", "Kurt Cobain"};
ayStartDates_ = new String[] {"01/19/1972", "06/01/1975", "12/31/1984"};
bandBean_ = new BandBean();
// 2. Fire up Hibernate
/*
All of the mappings are added to an instance of
net.sf.hibernate.cfg.Configuration, which is then
used to create the SessionFactory instance.
*/
Configuration cfg = new Configuration()
.addClass(BandBean.class)
.addClass(RecordingBean.class);
SessionFactory sf = cfg.buildSessionFactory();
// 3. Open Session
hSession_ = sf.openSession();
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
protected void tearDown() throws HibernateException {
bandBean_ = null;
// 5. close Session
hSession_.close();
}
/**
* Tests adding bands to the DB.
*/
public void testBandsAdd() throws HibernateException, ParseException {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// 4. open transaction, save bean(s), commit transaction
hTransaction_ = hSession_.beginTransaction();
for (int i = 0; i < 3; i++) {
bandBean_ = new BandBean();
bandBean_.setName(ayNames_[i]);
bandBean_.setFounder(ayFounders_[i]);
bandBean_.setStartdate(df.parse(ayStartDates_[i]));
hSession_.save(bandBean_);
}
hTransaction_.commit();
List bands = hSession_.find("from relationships.BandBean");
assertEquals(3, bands.size());
}
/**
* Tests adding bands to the DB.
*/
public void testBandsGet() throws HibernateException {
// 4. issue query... no transaction necessary
List bands = hSession_.find("from relationships.BandBean as bb " +
"where bb.name <= ?", "L", Hibernate.STRING);
int size = bands.size();
for (int i = 0; i < size; i++) {
System.out.println(bands.get(i));
}
}
The Test RecordingBean Code:
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
protected void setUp() throws MappingException, HibernateException {
ayBandNames_ = new String[] {"Eagles", "Aerosmith", "Nirvana",
"Aerosmith", "Nirvana", "Eagles"};
ayTitles_= new String[] {"Desperado", "Toys In the Attic", "Smells Like Teen Spirit",
"Walk This Way", "So Happy", "Witchy Woman"};
ayRatings_ = new String[] {"AAA", "B", "B-",
"AAA", "B", "A"};
ayBandejb_names_ = new String[] {"Eagles", "Aerosmith", "Nirvana",
"Aerosmith", "Nirvana", "Eagles"};
recordingBean_ = new RecordingBean();
// 2. Fire up Hibernate
/*
All of the mappings are added to an instance of
net.sf.hibernate.cfg.Configuration, which is then
used to create the SessionFactory instance.
*/
Configuration cfg = new Configuration()
.addClass(BandBean.class)
.addClass(RecordingBean.class);
SessionFactory sf = cfg.buildSessionFactory();
// 3. Open Session
hSession_ = sf.openSession();
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
protected void tearDown() throws HibernateException {
recordingBean_ = null;
// 5. close Session
hSession_.close();
}
/**
* Tests adding recordings to the DB.
*/
public void testRecordingsAdd() throws HibernateException {
// 4. open transaction, save bean(s), commit transaction
hTransaction_ = hSession_.beginTransaction();
for (int i = 0; i < 6; i++) {
//recordingBean_ = new RecordingBean(
// new RecordingBeanId(ayBandNames_[i], ayTitles_[i]));
recordingBean_ = new RecordingBean();
recordingBean_.setBandname(ayBandNames_[i]);
recordingBean_.setTitle(ayTitles_[i]);
recordingBean_.setRating(ayRatings_[i]);
recordingBean_.setBandejb_name(ayBandejb_names_[i]);
hSession_.save(recordingBean_);
}
hTransaction_.commit();
List recordings = hSession_.find("from relationships.RecordingBean");
assertEquals(6, recordings.size());
}
/**
* Tests adding bands to the DB.
*/
public void testRecordingsGet() throws HibernateException {
// 4. issue query... no transaction necessary
List recordings =
hSession_.find("from relationships.RecordingBean as rb " +
"where rb.bandname <= ?", "L", Hibernate.STRING);
int size = recordings.size();
for (int i = 0; i < size; i++) {
System.out.println(recordings.get(i));
}
}
Thanks.
|