I am trying to map two tables using hibernate one-to-many mapping. But, always it is mapping with the wrong column. Please any body help me on this.
Level4_Master [level_id (PK/Auto Increament), company_id, level_name, next_level_id, ts]
Level3_Master [level_id (PK/Auto Increament), prv_level_id, level_name, next_level_id, ts]
Above are the two tables I am using for the mapping. Here, I wanted to map column next_level_id of Level4_Master with column prv_level_id of Level3_Master. But, hibernate always mapping the column prv_level_id with level_id of Level4_Master as FK. My mapping files are:
<hibernate-mapping>
<class name="com.pojo.Level4" table="Level4_Master">
<id name="levelId" type="java.lang.Integer">
<column name="level_id" />
<generator class="increment" />
</id>
<property name="companyId" >
<column name="company_id" length="10" not-null="true" unique="true" />
</property>
<property name="levelName">
<column name="level_name" length="20" not-null="true" unique="true" />
</property>
<property name="nextLevelId" type="java.lang.Integer">
<column name="next_level_id" />
</property>
<set name="levelList" table="Level3_Master" inverse="true" lazy="true" fetch="select">
<key>
<column name="prv_level_id" not-null="true" />
</key>
<one-to-many class="com.pojo.Level3" />
</set>
</class>
</hibernate-mapping>
and
<hibernate-mapping>
<class name="com.pojo.Level3" table="Level3_Master">
<id name="levelId" type="java.lang.Integer">
<column name="level_id" />
<generator class="increment" />
</id>
<property name="prvLevelId" >
<column name="prv_level_id" length="10" not-null="true" unique="true" />
</property>
<property name="levelName">
<column name="level_name" length="20" not-null="true" unique="true" />
</property>
<property name="nextLevelId" type="java.lang.Integer">
<column name="next_level_id" />
</property>
<set name="levelList" table="Level2_Master" inverse="true" lazy="true" fetch="select">
<key>
<column name="prv_level_id" not-null="true" />
</key>
<one-to-many class="com.pojo.Level2" />
</set>
</class>
</hibernate-mapping>
and my pojo classes are like
class Level4{
private int levelId;
private int companyId;
private String levelName;
private int nextLevelId;
private Set<Level3> levelList = new HashSet<Level3>(0);
private Timestamp ts;
//getter n setter
}
class Level3{
private int levelId;
private int prvLevelId;
private String levelName;
private int nextLevelId;
private Set<Level2> levelList = new HashSet<Level2>(0);
private Timestamp ts;
//getter n setter
}
You need to use property-ref attribute with the property name. The property must add unique="true" in the property mapping.
Example -
<hibernate-mapping><class name="com.pojo.Level4" table="Level4_Master">
<id name="levelId" type="java.lang.Integer">
<column name="level_id" />
<generator class="increment" />
</id>
<property name="companyId" >
<column name="company_id" length="10" not-null="true" unique="true" />
</property>
<property name="levelName">
<column name="level_name" length="20" not-null="true" unique="true" />
</property>
<property name="nextLevelId" type="java.lang.Integer" unique="true">
<column name="next_level_id" />
</property>
<set name="levelList" table="Level3_Master" inverse="true" lazy="true" fetch="select">
<key>
<column name="prv_level_id" not-null="true" property-ref="nextLevelId"/>
</key>
<one-to-many class="com.pojo.Level3" />
</set>
Related
I am getting following issue while saving entity in hibernate -
It duplicates the record with null values -
(2661956,2601555,'Chloe','Chloe','Thooks',null,null,null,null,null,null,'Y','N','XYZ',to_date('15-NOV-16','DD-MON-RR'),null,null)
with a duplicate -
(2661946,2601555,null,null,null,null,null,null,null,null,null,'Y','N','XYZ',to_date('15-NOV-16','DD-MON-RR'),null,null) -
We have the following mapping -
Parent -
<?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 default-lazy="false">
<class name="com.sm.persistence.LOADetailBO" table="LOADETAILS">
<id name="loaDetailsId" type="long">
<column name="LOA_ID" precision="38" scale="0"/>
<generator class="com.sm.persistence.dao.seqgen.LOADetailSeqGen">
</generator>
</id>
<many-to-one name="reregistration" class="com.sm.persistence.RerBO" fetch="join">
<column name="RER_ID" precision="38" scale="0"/>
</many-to-one>
<property name="relatedPlanManager" type="string">
<column name="RELATED_PLAN_MANAGER"/>
</property>
<property name="relatedPlanManagerCode" type="string">
<column name="RELATED_PLAN_MANAGER_CODE"/>
</property>
<set name="relatedPlanManagerAddress" inverse="true" cascade="all-delete-orphan">
<key>
<column name="LOA_ID" precision="38" scale="0" not-null="false"/>
</key>
<one-to-many class="com.sm.persistence.AddressBO" />
</set>
<set name="corporateCustomers" inverse="true" cascade="all-delete-orphan">
<key>
<column name="LOA_ID" precision="38" scale="0" not-null="false"/>
</key>
<one-to-many class="com.sm.persistence.CorporateCustomerBO" />
</set>
<set name="privateCustomers" inverse="true" cascade="all-delete-orphan">
<key>
<column name="LOA_ID" precision="38" scale="0" not-null="false" />
</key>
<one-to-many class="com.sm.persistence.PrivateCustomerBO"/>
</set>
<set name="unwrappedAccount" inverse="true" cascade="all-delete-orphan">
<key>
<column name="LOA_ID" precision="38" scale="0" not-null="false"/>
</key>
<one-to-many class="com.sm.persistence.UnwrappedAccountBO" />
</set>
<set name="wrappedAccount" inverse="true" cascade="all-delete-orphan">
<key>
<column name="LOA_ID" precision="38" scale="0" not-null="false" />
</key>
<one-to-many class="com.sm.persistence.WrappedAccountBO"/>
</set>
</class>
</hibernate-mapping>
Child -
<?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 default-lazy="false">
<class name="com.sm.persistence.PrivateCustomerBO" table="RER_CUSTOMER_DETAIL">
<id name="privateCustomerId" type="long">
<column name="RERE_CUSTOMER_ID" precision="38" scale="0"/>
<generator class="com.sm.persistence.dao.seqgen.PrivateCustomerSeqGen">
</generator>
</id>
<many-to-one name="loaDetail" class="com.sm.persistence.LOADetailBO" fetch="join">
<column name="LOA_DETAILS_ID" precision="38" scale="0"/>
</many-to-one>
<property name="title" type="string">
<column name="TITLE"/>
</property>
<property name="firstName" type="string">
<column name="FIRST_NAME"/>
</property>
<property name="surname" type="string">
<column name="SURNAME"/>
</property>
<set name="address" inverse="false" cascade="all-delete-orphan">
<key>
<column name="PRIVATE_CUSTOMER_ID" precision="38" scale="0" not-null="false"/>
</key>
<one-to-many class="com.sm.persistence.AddressBO" />
</set>
<property name="primary" type="string">
<column name="IS_PRIMARY"/>
</property>
<property name="corporate" type="string">
<column name="IS_CORPORATE"/>
</property>
<!--<property name="dateofBirth" type="string">
<column name="DATE_OF_BIRTH"/>
</property>-->
<property name="nationalInsurance" type="string">
<column name="NINO"/>
</property>
</class>
</hibernate-mapping>
I checked parent object both pre and post save and can't find any object with null value populated. However, when I fetch the object hierarchy with criteria api it returns duplicate record with null.
Please guide.
Quick answer:
PrivateCustomerBO is a child of LOADetailBO. So when your java code
saves a PrivateCustomerBO object it will write to all tables
specified against these 2 objects: RER_CUSTOMER_DETAIL = primary
table for PrivateCustomerBO, LOADETAILS = primary table for
LOADetailBO, plus all tables referenced by these two objects via
many-to-one or one-to-many mappings. CORRECT BEHAVIOUR
Expanding the bolded bit above, LOADetailBO is referenced by
PrivateCustomerBO via:
<many-to-one name="loaDetail" class="com.sm.persistence.LOADetailBO" fetch="join">
<column name="LOA_DETAILS_ID" precision="38" scale="0"/>
</many-to-one>
This means that LOADETAILS is additionally referenced as a related
table, not just a parent and a second row is written (in this case
there is some data missing (NULL) because your program hasn't
populated it for the referenced entity). INCORRECT BEHAVIOUR.
Fix: remove the many-to-one mapping shown above (it's redundant and covered by parent).
I am trying to insert data to the DB using hibernate . Here is how I going perform that action
session.beginTransaction();
pojo.StuDetails stu = new StuDetails();
stu.setFName(f_name);
stu.setLName(l_name);
stu.setSex(sex);
stu.setDob(dob);
pojo.Subject sub = new Subject(subject, day, time);
pojo.SubjectHasStuDetails shs = new SubjectHasStuDetails(stu, sub);
session.save(shs);
session.getTransaction().commit();
But It gives me an error saying
Exception in thread "main"
org.hibernate.TransientPropertyValueException: Not-null property
references a transient value - transient instance must be saved before
current operation
Here is my student details entity
public class StuDetails implements java.io.Serializable {
private Integer id;
private String FName;
private String LName;
private String sex;
private String dob;
private Set subjectHasStuDetailses = new HashSet();
...
//constructors and getters, setters
My StudentDetails hbm.xml
<hibernate-mapping>
<class name="pojo.StuDetails" table="stu_details" catalog="laravel_test" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="FName" type="string">
<column name="f_name" length="45" not-null="true" />
</property>
<property name="LName" type="string">
<column name="l_name" length="45" not-null="true" />
</property>
<property name="sex" type="string">
<column name="sex" length="45" not-null="true" />
</property>
<property name="dob" type="string">
<column name="dob" length="45" not-null="true" />
</property>
<set name="subjectHasStuDetailses" table="subject_has_stu_details" inverse="true" lazy="true" fetch="select">
<key>
<column name="stu_details_id" not-null="true" />
</key>
<one-to-many class="pojo.SubjectHasStuDetails" />
</set>
</class>
</hibernate-mapping>
My Subject Entity looks like
public class Subject implements java.io.Serializable {
private Integer id;
private String subName;
private String day;
private String time;
private Set subjectHasStuDetailses = new HashSet();
...
//constructors and getters, setters
Subject.hbm.xml
<hibernate-mapping>
<class name="pojo.Subject" table="subject" catalog="laravel_test" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="subName" type="string">
<column name="sub_name" length="45" not-null="true" />
</property>
<property name="day" type="string">
<column name="day" length="45" not-null="true" />
</property>
<property name="time" type="string">
<column name="time" length="45" not-null="true" />
</property>
<set name="subjectHasStuDetailses" table="subject_has_stu_details" inverse="true" lazy="true" fetch="select">
<key>
<column name="subject_id" not-null="true" />
</key>
<one-to-many class="pojo.SubjectHasStuDetails" />
</set>
</class>
</hibernate-mapping>
Here is the SubjetcHasStuDetails Entity
public class SubjectHasStuDetails implements java.io.Serializable {
private Integer id;
private StuDetails stuDetails;
private Subject subject;
...
//constructors and getters, setters
SubjectHasStuDetials.hbm.xml
<hibernate-mapping>
<class name="pojo.SubjectHasStuDetails" table="subject_has_stu_details"
catalog="laravel_test" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="stuDetails" class="pojo.StuDetails" fetch="select">
<column name="stu_details_id" not-null="true" />
</many-to-one>
<many-to-one name="subject" class="pojo.Subject" fetch="select" >
<column name="subject_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Can someone help me on this error please ... Thanks..
In your SubjectHasStuDetials.hbm.xml make these changes :
<many-to-one name="stuDetails" class="pojo.StuDetails" fetch="select" cascade="all">
<column name="stu_details_id" not-null="true" />
</many-to-one>
<many-to-one name="subject" class="pojo.Subject" fetch="select" cascade="all" >
<column name="subject_id" not-null="true" />
</many-to-one>
Add cascade="all" attribute to both stuDetails and subject many-to-one tags.
Cascade attribute is mandatory, when ever we apply relationship
between objects, cascade attribute transfers operations done on one
object onto its related child objects
If we write cascade = “all” then changes at parent class object will
be effected to child class object too, if we write cascade = “all”
then all operations like insert, delete, update at parent object will
be effected to child object also.
Example: if we apply insert(or update or delete) operation on parent
class object, then child class objects will also be stored into the
database.
I would like to map the same key column, parent_id this example, across the subclasses. Both ClientProfileDO and BusinessProfileDO are inherited from UserProfileDO. They are on the same table, USERPROFILE. So, they refer the same foreign key on the Contact table. Is it logically incorrect?
<hibernate-mapping package="com.rentorama2.frontpage.client.serialize"default-lazy="false">
<class name="UserProfileDO" table="USERPROFILE">
<id name="oid" type="long" column="oid" >
<generator class="increment">
<param name="initial_value">1</param>
</generator>
</id>
<discriminator column="dcolumn" type="string" length="5"/>
<property name="acctOid">
<column name="acctOid" />
</property>
<property name="email">
<column name="email"/>
</property>
<subclass name="ClientProfileDO" discriminator-value="CP">
<list name="c_contacts" cascade="all">
<key column="parent_id" not-null="true"/>
<index column="idx"/>
<one-to-many class="Contact"/>
</list>
</subclass>
<subclass name="BusinessProfileDO" discriminator-value="BP">
<property name="b_updateAnnouncement">
<column name="updateAnnouncement"/>
</property>
<list name="b_contacts" cascade="all">
<key column="parent_id" not-null="true"/>
<index column="idx"/>
<one-to-many class="Contact"/>
</list>
</subclass>
</class>
I think you should move the Contact one-to-many association into UserProfileDO base class:
<hibernate-mapping package="com.rentorama2.frontpage.client.serialize"default-lazy="false">
<class name="UserProfileDO" table="USERPROFILE">
<id name="oid" type="long" column="oid" >
<generator class="increment">
<param name="initial_value">1</param>
</generator>
</id>
<discriminator column="dcolumn" type="string" length="5"/>
<property name="acctOid">
<column name="acctOid" />
</property>
<property name="email">
<column name="email"/>
</property>
<list name="contacts" cascade="all">
<key column="parent_id" not-null="true"/>
<index column="idx"/>
<one-to-many class="Contact"/>
</list>
<subclass name="ClientProfileDO" discriminator-value="CP">
</subclass>
<subclass name="BusinessProfileDO" discriminator-value="BP">
<property name="b_updateAnnouncement">
<column name="updateAnnouncement"/>
</property>
</subclass>
</class>
And the UserProfileDO class will have a Contact list:
private List<Contact> contacts = new ArrayList<>();
public List<Contact> getContacts() {
return contacts;
}
public void getContacts(List<Contact> contacts) {
this.contacts = contacts;
}
I just need to add an entry on a many to many table/object with hibernate (associate a song to a playlist) called SongsPlaylist.
the structure of the tables is so defined: Songs---->SongPlaylist<----Playlists
the mapping files (omitted not important portion of file):
<hibernate-mapping>
<class name="model.pojo.Songs" table="songs" catalog="dbname" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<set name="songsPlaylists" table="songs_playlist" inverse="true" lazy="true" fetch="select">
<key>
<column name="song" not-null="true" unique="true" />
</key>
<one-to-many class="model.pojo.SongsPlaylist" />
</set>
</class>
</hibernate-mapping>
<!-- Generated 24-set-2014 8.40.14 by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
<class name="model.pojo.SongsPlaylist" table="songs_playlist" catalog="dbname" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="songs" class="model.pojo.Songs" fetch="select">
<column name="song" not-null="true" unique="true" />
</many-to-one>
<many-to-one name="playlists" class="model.pojo.Playlists" fetch="select">
<column name="playlist" not-null="true" unique="true" />
</many-to-one>
</class>
</hibernate-mapping>
<!-- Generated 24-set-2014 8.40.14 by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
<class name="model.pojo.Playlists" table="playlists" catalog="dbname" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<set name="songsPlaylists" table="songs_playlist" inverse="true" lazy="true" fetch="select">
<key>
<column name="playlist" not-null="true" unique="true" />
</key>
<one-to-many class="model.pojo.SongsPlaylist" />
</set>
</class>
</hibernate-mapping>
the pojo classes:
public class SongsPlaylist implements java.io.Serializable {
private Integer id;
private Songs songs;
private Playlists playlists;
(...)
}
public class Songs implements java.io.Serializable {
private Integer id;
private Set songsPlaylists = new HashSet(0);
(...)
}
public class Playlists implements java.io.Serializable {
private Integer id;
private Set songsPlaylists = new HashSet(0);
(...)
}
this is my function
s = new HibernateUtil().getSessionFactory().openSession();
transaction = s.beginTransaction();
(...)
q = s.createQuery("select sp from SongsPlaylist sp where sp.playlists = :p");
q.setParameter("p", p);
sp = q.list();
//already exists a song binded to that playlist
if(sp.size()>0){
Set<Playlists> sP = sp.get(0).getPlaylists().getSongsPlaylists();
sP.add(p);
song.setSongsPlaylists(sP);
s.save(s);
}else{
//or it is in another playlist...
q = s.createQuery("select sp from SongsPlaylist sp where sp.songs = :s");
q.setParameter("s", song);
sp = q.list();
if(sp.size()>0){
Set<Songs> sP = sp.get(0).getSongs().getSongsPlaylists();
sP.add(song);
p.setSongsPlaylists(sP);
s.save(p);
}else{
s.save(new SongsPlaylist(song, p));
}
}
}
transaction.commit();
s.close();
and it stucks on the transaction.commit(); (last lines) with
org.hibernate.HibernateException: Found shared references to a collection:
model.pojo.Songs.songsPlaylists
it looks like I do something wrong when persisting a Set<Playlists (the same when I try to do that with Set<Songs>) on a persistent object. what 's the best practice for this case in order to insert a "SongPlaylist" and to avoid the hibernate exception mentioned?
found a solution...
in the else block (instead of what I wrote), now is:
Set<Playlists> sP = song.getPlaylistses(); // terrific netbeans naming when automatic pojo mapping :-)
sP.add(p);
song.setPlaylistses(sP);
s.save(song);
and just a side bug, not related to this one, but
SongsPlaylist should not be as hbm file. hibernate mapping should be instead
<hibernate-mapping>
<class name="model.pojo.Songs" table="songs" catalog="dbname" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
(...)
<set name="playlistses" table="songs_playlist" inverse="false" lazy="false" fetch="select">
<key>
<column name="song" not-null="true" />
</key>
<many-to-many entity-name="model.pojo.Playlists">
<column name="playlist" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="model.pojo.Playlists" table="playlists" catalog="dbname" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<set name="songses" table="songs_playlist" inverse="false" lazy="false" fetch="select">
<key>
<column name="playlist" not-null="true" />
</key>
<many-to-many entity-name="model.pojo.Songs">
<column name="song" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
I have two tables stock and category having many-to-many relationship each other.
Stock.hbm.xml
<hibernate-mapping>
<class name="hibernate.mapping.manytomany.Stock" table="stock">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="identity" />
</id>
<property name="stockCode" type="string">
<column name="STOCK_CODE" length="10" not-null="true" unique="true" />
</property>
<property name="stockName" type="string">
<column name="STOCK_NAME" length="20" not-null="true" unique="true" />
</property>
<set name="categories" table="stock_category"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="STOCK_ID" not-null="true" />
</key>
<many-to-many entity-name="hibernate.mapping.manytomany.Category">
<column name="CATEGORY_ID" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Category.hbm.xml
<hibernate-mapping>
<class name="hibernate.mapping.manytomany.Category" table="category">
<id name="categoryId" type="java.lang.Integer">
<column name="CATEGORY_ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="NAME" length="10" not-null="true" />
</property>
<property name="desc" type="string">
<column name="[DESC]" not-null="true" />
</property>
<set name="stocks" table="stock_category" inverse="true" lazy="true"
fetch="select">
<key>
<column name="CATEGORY_ID" not-null="true" />
</key>
<many-to-many entity-name="hibernate.mapping.manytomany.Stock">
<column name="STOCK_ID" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
And this is my criteria query,
Criteria c = session.createCriteria(Category.class, "c");
c.createAlias("c.stocks", "s");
c.add(Restrictions.eq("c.categoryId", 1));
c.setProjection(Projections.projectionList()
.add(Projections.property("s.stockId"))
.add(Projections.property("s.stockName")));
I need equivalent HQL for this scenario..I've tried this but it gives different result,
String query = "select c.stocks.stockId, c.stocks.stockName from Category c where
c.categoryId=1"
Let me know if you need more details.
OK, so apparently, you missed the section about joins in the documentation:
select s.stockId, // equivalent to the s.stockId projection
s.stockName // equivalent to the s.stockName projection
from Category c // equivalent to the root criteria creation
join c.stocks s // equivalent to the alias creation
where c.categoryId = 1 // equivalent to the restriction addition
I am planning to investigate this issue thoroughly. What i doubt from previous answer for now is there should be ON Clause in join statement. Pardon me if my expectation is wrong.
select s.stockId,s.stockName from Category c join c.stocks s
on c.stockId=s.stockId where c.categoryId = 1 ;
//it might be on c.CATEGORY_ID=s.CATEGORY_ID also