I have following entities.
Code:
public class Guitar {
private long serialNumber;
private int price;
GuitarSpec specification;
}
public class GuitarSpec {
private long specId;
private String builder;
private String model;
private String type;
private int numStrings;
private String backWood;
private String topWood;
}
<hibernate-mapping package="com.transcend.music.entity">
<class name="Guitar" table="t_guitar">
<id name="serialNumber" type="long">
<generator class="native"/>
</id>
<property name="price" type="integer" />
<many-to-one name="specification" column="specification_id"
class="GuitarSpec" cascade="all" not-null="true" />
</class>
</hibernate-mapping>
<hibernate-mapping package="com.transcend.music.entity">
<class name="GuitarSpec" table="t_guitar_specs">
<id name="specId" type="long">
<generator class="native"/>
</id>
<property name="builder" type="string" length="100" not-null="false"/>
<property name="model" type="string" not-null="false"/>
<property name="type" type="string" not-null="false"/>
<property name="numStrings" type="integer" not-null="false"/>
<property name="backWood" type="string" not-null="false"/>
<property name="topWood" type="string" not-null="false"/>
</class>
</hibernate-mapping>
i.e. there is many(Guitar) to one(GuitarSpec) relation. I am fetching a row from GuitarSpec(these rows are unique). Now i want to fetch rows from Guitar(Column specification_id references the specId from GuitarSpec).
I am trying the following but it does not work.
Code:
Query query = session
.createQuery("from GuitarSpec where builder=:builder and model=:model and type=:type and numStrings=:numStrings and backWood=:backWood and topWood=:topWood");
query.setParameter("builder", spec.getBuilder());
query.setParameter("model", spec.getModel());
query.setParameter("type", spec.getType());
query.setParameter("numStrings", spec.getNumStrings());
query.setParameter("backWood", spec.getBackWood());
query.setParameter("topWood", spec.getTopWood());
List results = query.list();
GuitarSpec guitarSpec = null;
Guitar guitar;
Long specId=0L;
if (results.size() > 0)
{
specId = new Long((((GuitarSpec) results.get(0)).getSpecId()));
guitarSpec = (GuitarSpec) session.get(GuitarSpec.class, specId);
}
Query queryGuitar = session
.createQuery("from Guitar where specification=:specification");
query.setParameter("specification", guitarSpec);
List guitars = queryGuitar.list();
I am able to fetch GuitarSpec , but not able to fetch Guitar. How can i fetch Guitar.
Thanks.