what type of mapping is this? - java

With the hibernate mapping file as shown :
<hibernate-mapping>
<class name="pojo.Ghazal" table="ghazal">
<id name="s_no">
<generator class="increment" />
</id>
<property name="poem" />
<property name="poet" />
<map name="map" table="linked">
<key column="s_no" />
<index column="key_" type="string" />
<element column="val_" type="string" />
</map>
</class>
</hibernate-mapping>
what type of mapping it is ?
The pojo named ghazal has the following properties :
s_no
poem
poet
map
I have heard many types of mapping like many to one, one to one, etc etc.

This is an association done with collection(Map named "map" in your case) of values, relationship is Many to many, you can find the detailed docs here

Related

Mapping exception says, could not instantiate id generator. Why this exception?

While trying to achieve one to many mapping for class Person and Address I get the following exception :org.hibernate.MappingException: could not instantiate id generator.
I don't know the reason for this. What could be the reason I am getting this exception ?
<class name="pojo.Person" table="person">
<id name="personID" column="p_id">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="address" cascade="all">
<key column="p_id" />
<one-to-many class="pojo.Address" />
</set>
</class>
<class name="pojo.Address" table="address">
<id name="a_id" column="a_id">
<generator class="foreign" />
</id>
<property name="personAddress" column="p_address" />
</class>
Sql that created table:
CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id));
CREATE TABLE address(a_id INTEGER,p_address TEXT);
Note: One person can have more than one address
You need to change generator class from foreigner to increment in a key of your Address entity. For details, see this answer where I already mentioned that.

What shall I mention for the 'name' attribute ? Why this validation error?

I am trying to make a one-to-one relation between Country class and PM class.This is the xml mapping I tried to
make.But there is something wrong with the xml as the error says.
Attribute "name" is required and must be specified for element type "one-to-one".
I do not understand this. What shall I mention for the name attribute ? I mentioned the class with which I am trying to
make a one-to-one relation.
<class name="pojo.Country" table="country">
<id name="c_id">
<generator class="increment" />
</id>
<property name="name_c" />
<one-to-one class="pojo.PM" />
</class>
<class name="pojo.PM" table="pm">
<id name="c_id">
<generator class="increment" />
</id>
<property name="name_pm" />
</class>
There are two tables named pm and country. The relation I am trying to make is that one country can have one PM and PM can belong to one country.
You should specify to which attribute you're making this relationship.
For example for country:
country_id, country_name, country_PM (which has pm_ids in it)
Try this:
<class name="pojo.Country" table="country">
<id name="c_id">
<generator class="increment" />
</id>
<property name="name_c" />
<one-to-one class="pojo.PM" name="pm_id" foreign_key="c_id"/>
</class>
if that doesn't work. Try property-ref instead of foreign-key attribute

hibernate: how to create a composite foreign key in xml?

I have a class Event containing a composite primary key (start date and end date).
A EventPlanning class holds a Set of such Event objects and has to persist them using hibernate with XML.
I can do this for classes with a common primary key:
<!-- EventPlanning xml -->
....
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name" type="string" update="false" />
<set name="events" table="events" cascade="all">
<key column="event_id"> // ###### here! ######
</key>
<one-to-many class="myPackage.Event" />
</set>
...
but I can't find out how this works with a composite key..
replacing the <key column="event_id"> with the following code doesn't work:
<key>
<property column="start_date" />
<property column="end_date" />
</key>
I'd be glad if somebody can show me the right syntax! :)
the Event xml looks like this:
<class name="myPackage.Even" table="events">
<composite-id>
<key-property name="startDate" column="start_date" type="date" />
<key-property name="endDate" column="end_date" type="date" />
</composite-id>
<property name="signinDeadline" column="signin_deadline"
type="date" />
<property name="confirmationDeadline" column="confirmation_deadline"
type="date" />
<set name="participants" table="participants" cascade="all">
<key column="event_id">
</key>
<one-to-many class="myPackage.Participants" />
</set>
</class>
thanks in advance! :)
Something like this works for me:
<class name="YourClass" table="your_table" ...>
<composite-id name="compositeId" class="DoubleDate">
<key-property name="start_date" column="start_date"/>
<key-property name="end_date" column="end_date"/>
</composite-id>
...
</class>
public class DoubleDate implements Serializable {
private Date start_date, end_date;
public DoubleDate() {
}
// setters, getters
}
public class YourClass {
private DoubleDate compositeId;
// public no args ctr, getters, setters, etc
}
After having now worked longer with JPA and Hibernate, I'd just say that you simply should not use composite primary keys. Caches use ids as keys that point to cached values, data retrieving methods like get and load expect the id as parameter etc.
The advantages gained by having an id field pay off against the additional space it needs.

Making join with Criteria

I'm terribly new to Hibernate. I've googled for two hours but I still can't figure out, how to make JOIN without using HQL, only by criteria. I have tables Clients(cID, name) and Visits(vID, vcID, date). The relation is one to many (one client can visit multiple times). I would also like to do it without setFetchMode. Just Criteria. Do I have to change the mappping xml?
UPDATE:
this is part of my mapping xml:
<class name="Client" table="Clients">
<id name="cID" column="cID"><generator class="native"/></id>
<property name="name" length="10" not-null="true"/>
</class>
<class name="Visit" table="Visits">
<id name="vID" column="vID"><generator class="native"/></id>
<property name="vcID" length="10" not-null="true"/>
<property name="date" length="25" not-null="true"/>
</class>
Having a class Client with a list-attribute "visits" that's mapping to your Visit-Entity:
Criteria criteria = session.createCriteria(Client.class);
criteria.addCriteria("visits");
This would create an inner join between your client-table and your visits-table.
Update:
Here you'll find some good examples: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html#querycriteria-associations
Mapping Example
I hardly ever use hibernate mapping xml, however it should read similiar to:
<class name="Client" table="Clients">
<id name="cID" column="cID"><generator class="native"/></id>
<property name="name" length="10" not-null="true"/>
<bag name="visits">
<key column="vcId"/>
<one-to-many class="Visit"/>
</bag>
</class>
Tell Hibernate that there is a property "visits" which represents a one-to-many relationship.
You need to update you mapping:
<class name="Client" table="Clients">
<id name="cID" column="cID"><generator class="native"/></id>
<property name="name" length="10" not-null="true"/>
<!-- Declare Set<Visit> visits in the Client class-->
<set name="visits" lazy="false" cascade="all">
<key column="vcID"/>
<one-to-many class="your.package.Visit"/>
</set>
</class>
<class name="Visit" table="Visits">
<id name="vID" column="vID"><generator class="native"/></id>
<!-- and add "Client client" property to your Visit class -->
<many-to-one name="client" column="vcID" lazy="false"/>
<property name="date" length="25" not-null="true"/>
</class>
Then:
Criteria criteria = session.createCriteria(Visit.class).addCriteria("client")
.add(Restriction.eq(...));
or
Criteria criteria = session.createCriteria(Client.class).addCriteria("visits")
.add(Restriction.eq(...));
And Hibernate will join them automatically.

Map ArrayList with Hibernate

I just coded my first Hibernate examples.
The database connection works and I understand how I can map a String from a POJO to a database field:
private String firstName;
And in the mapping file:
<property name="firstName" type="java.lang.String">
<column name="FIRSTNAME" />
</property>
But how can I map an ArrayList to the database? A simpl example from the mapping xml file would be appreciated.
Cheers
UPDATE
I switched to List instead of ArrayList found an example. Now I map as follows:
<list name="test" inverse="false" table="CONTACT" lazy="true">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<element type="java.lang.String">
<column name="TEST" />
</element>
</list>
Unfortunately, I get an exception that I do not understand:
Exception in thread "main" org.hibernate.MappingException: Foreign key (FK6382B0003257FF7F:CONTACT [ID])) must have same number of columns as the referenced primary key (CONTACT [ID,idx])
Any ideas?
Cheers
I notice that you are using XML to map your POJOs. You will find some information about that here.
for example:
<list name="myArrayListProperty" cascade="all">
<key column="parent_id"/>
<index column="idx"/>
<one-to-many class="WhatIsInTheList"/>
</list>
However, using annotations have some advantages. This link will explain how to map any collection using annotations.
See the collection mapping section of the docs. There are multiple ways to map a list (one-to-many, many-to-many, a collection of elements). You can map it as a list or as a bag, so read the whole section.
You have a little error in the XML configuration:
When you have a list the solution to map this list using a database is to link with a additional table, so instead of doing:
<list name="test" inverse="false" table="CONTACT" lazy="true">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<element type="java.lang.String">
<column name="TEST" />
</element>
</list>
You should have to do map to a new data table that holds the list values:
<list name="test" inverse="false" table="CONTACT_test" lazy="true">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<element type="java.lang.String">
<column name="TEST" />
</element>
</list>
Hibernate automatically creates the new table for you.

Categories

Resources