how to set Multiple one to many relation with same column hibernate - java

There are two entities.
Route (Arrival, ArrivalID and Departure , DepartureID) as Location
Location (Arrivals , Departures ) as Route
Location will have 2 one to many relationship with route table.
I am trying to set.
Route.xml
<many-to-one name="departure" class="com.nakisa.agency.Location" fetch="select" insert="false" update="false">
<column name="locationID" not-null="true" />
</many-to-one>
<many-to-one name="arrival" class="com.nakisa.agency.Location" fetch="select" insert="false" update="false">
<column name="locationID" not-null="true" />
</many-to-one>
Location.xml
<set name="arrivals" table="Routes" inverse="true" lazy="true" fetch="select">
<key>
<column name="arrivalID" not-null="true" />
</key>
<one-to-many class="com.nakisa.agency.Route" />
</set>
<set name="departures" table="Routes" inverse="true" lazy="true" fetch="select">
<key>
<column name="departureID" not-null="true" />
</key>
<one-to-many class="com.nakisa.agency.Route" />
</set>
I'm getting error as departureID is null even i set departureID there in route.
How to correct these mappings in order to work

I think you have problem with fetching:
Lazy="true|false" controls whether an association is loaded eagerly or on demand.
fetch="select|subselect|join|batch" controls how is that entity or collection loaded, when it's required to be loaded.
With lazy="true" and fetch="select" Hibernate will lazy load the collection and will load it with a select. If you set lazy="false", the same select will be executed, the difference will be that it will get executed eagerly. Hope this makes sense.
Try to set lazy="false", and then see if your departureID is still null.

Related

Fetching data only for specified mapping in HQL query

I have following hbm.xml file
<hibernate-mapping>
<class catalog="test_user" name="test.user" table="user">
<id name="id" type="java.lang.Long">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="name" type="string">
<column length="200" name="name" unique="true"/>
</property>
<set fetch="join" inverse="true" lazy="true" name="education" table="user_education">
<key>
<column name="aid"/>
</key>
<one-to-many class="test.UserEducation"/>
</set>
<set fetch="join" inverse="true" lazy="true" name="employment" table="user_employment">
<key>
<column name="aid"/>
</key>
<one-to-many class="test.UserEmployment"/>
</set>
<set fetch="join" inverse="true" lazy="false" name="otherProfiles" table="user_other_profile">
<key>
<column name="aid"/>
</key>
<one-to-many class="test.OtherProfile"/>
</set>
<set fetch="join" inverse="true" lazy="false" name="settings" table="user_setting">
<key>
<column name="aid"/>
</key>
<one-to-many class="test.Setting"/>
</set>
<set fetch="join" inverse="true" lazy="false" name="images" table="user_images">
<key>
<column name="aid"/>
</key>
<one-to-many class="test.Images"/>
</set>
..
..
...
and many tables associate with user here I am using fetch="join"
for maximum tables.
In other queries I have to fetch data from few other related tables so I made
fetch="join".
I want to execute quest
from user as u
left join fetch u.settings
where u.id=25
My problem is when I want to fetch data from user it always fetch data from all associated table where fetch="join".
I want to know how to fetch only related join data. For above query only data from user and settings data should be fetched not from other tables when we have specified fetch="join" for multiple tables.
You should not use fetch="join" because EAGER fetching can lead to Cartesian Products and it's not very efficient.
You need to set those associations to lazy="true" and only fetch themon a query basis:
from user as u
left join fetch u.settings
where u.id=25
This way, you will only fetch Users along with their settings and without join fetching the other associations.

Foreign key in compiste primary key (duplicated column error)

We are just integrating Hibernate in our web application which has been developed since today using direct queries to a relational database.
We have generated hbm.xml files in order to have all the entities and mappings implemented for accessing database via hibernate for reading and writing.
The thing is that our schema has many composite primary keys and, because of that, we have a lot of foreign keys refering to those composite ids.
Above you can see an example of database schema in which we are having many problems. There is also the hbm.xml file:
<class name="Profile" table="Profile" optimistic-lock="version">
<id name="profileId" type "java.lang.Integer">
<column name="profileId" />
<generator class="assigned" />
</id>
<many-to-one name="location" class="Location" fetch="select" >
<column name="locationId" length="3" not-null="true" />
</many-to-one>
<many-to-one name="users" class="Users" fetch="select">
<column name="locationId" length="3" not-null="true" />
<column name="userId" length="30" not-null="true" />
</many-to-one>
<many-to-one name="groupUsers" class="GroupUsers" fetch="select">
<column name="locationId" length="3" not-null="true" />
<column name="groupUserId" length="30" not-null="true" />
</many-to-one>
</class>
<class name="Users" table="Users" optimistic-lock="version">
<composite-id name="id" class="UsersId">
<key-property name="locationId" type="string">
<column name="locationId" length="3" />
</key-property>
<key-property name="userId" type="string">
<column name="userId" length="30" />
</key-property>
</composite-id>
<set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
<key>
<column name="locationId" length="3" not-null="true" />
<column name="userId" length="30" not-null="true" />
</key>
<one-to-many class="Profile" />
</set>
</class>
<class name="GroupUsers" table="GroupUsers" optimistic-lock="version">
<composite-id name="id" class="GroupUsersId">
<key-property name="locationId" type="string">
<column name="locationId" length="3" />
</key-property>
<key-property name="groupUserId" type="string">
<column name="groupUserId" length="30" />
</key-property>
</composite-id>
<set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
<key>
<column name="locationId" length="3" not-null="true" />
<column name="groupUserId" length="30" not-null="true" />
</key>
<one-to-many class="Profile" />
</set>
</class>
<class name="Location" table="Location" optimistic-lock="version">
<id name="locationId" type="string">
<column name="locationId" length="3" />
<generator class="assigned" />
</id>
<set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
<key>
<column name="locationId" length="3" not-null="true" />
</key>
<one-to-many class="Profile" />
</set>
</class>
When starting Tomcat I get the following error: "Profile column: locationId (should be mapped with insert="false" update="false")"
But if I try to put this two attributes in the many-to-one relation of the Profile.hbm.xml file, I can't insert a profile instance to the database (no effect):
<many-to-one name="location" class="Location" fetch="select" insert="false" update="false">
<column name="locationId" length="3" not-null="true" />
</many-to-one>
<many-to-one name="users" class="Users" fetch="select"insert="false" update="false">
<column name="locationId" length="3" not-null="true" />
<column name="userId" length="30" not-null="true" />
</many-to-one>
<many-to-one name="groupUsers" class="GroupUsers" fetch="select" insert="false" update="false">
<column name="locationId" length="3" not-null="true" />
<column name="groupUserId" length="30" not-null="true" />
</many-to-one>
Can anyone help me, please?
Thank you very much in advance.
Because you say you have many composite primary keys in a existing schema with data in it and you want to add hibernate to it.
That sounds like you rather would have no composite primary keys.
Just a thought:
I think it would not be such a big thing to drop your existing combined primary keys, make them uniqe indices instead and create new non combined primary keys updating all your data with a sequence (depending on the DMBS you are using).
That of course depends on how you want to integrate hibernate. There could be reasons of keeping the old combined primary keys.
But maybeyou should think about it.

What is the meaning of discriminator-value in Hibernate mapping file?

<class name="admin.model.OrganizationUnit" table="ORGANIZATION_UNIT" discriminator-value="admin.model.OrganizationUnit" dynamic-update="true">
<id name="ObjectId" type="string" column="object_id">
<generator class="assigned"/>
</id>
<discriminator column="discriminator"/>
<property name="ObjectType" type="string">
<column name="ObjectType" sql-type="varchar2(255)" not-null="true"/>
</property>
<subclass name="admin.model.DmSystem" discriminator-value="admin.model.DmSystem" dynamic-update="true">
<set name="ChildOrgs" lazy="true" table="ORGANIZATION_UNIT" where="ou_type_code='CWORG'">
<key column="system_org_id"/>
<one-to-many class="Dm.bizcomponent.admin.model.OrganizationUnit"/>
</set>
<set name="SystemAdminAccessGroup" lazy="true" inverse="true" table="CWGROUP" where="group_type_code='SYSTEM_ADMINACCESS'">
<key column="owner_id"/>
<one-to-many class="admin.model.Group"/>
</set>
</subclass>
</class>
Here is the mapping code
In the above code there is one parent Class OrganizationUnit with two subclass
The discriminator-value is used to determine the class type of the entity subclass type associated with a given database row entry in the base class database table.

Hibernate org.hibernate.MappingException... an associon ..to unmapped class- Many toMany

I have auto generated the code using netbeans to create a Hibernate configuration; so I have two tables mapped like this (Many-to-Many) :
<hibernate-mapping auto-import="true">
<class name="com.antoiovi.jobprograms.entity.Roles" table="roles" catalog="jobprograms">
<id name="rolesName" type="string">
<column name="roles_name" length="20" />
<generator class="assigned" />
</id>
<set name="userses" table="users_roles" inverse="true" lazy="false" fetch="select" cascade="all">
<key>
<column name="role_name" length="20" not-null="true" />
</key>
<many-to-many entity-name="com.antoiovi.jobprograms.entity.Users">
<column name="user_name" length="15" not-null="true" />
</many-to-many>
</set>
</class>
<hibernate-mapping auto-import="true">
<class name="com.antoiovi.jobprograms.entity.Users" table="users" catalog="jobprograms">
<id name="idusers" type="java.lang.Integer">
<column name="idusers" />
<generator class="identity" />
</id>
<property name="userName" type="string">
<column name="user_name" length="15" not-null="true" unique="true" />
</property>
<property name="userPass" type="string">
<column name="user_pass" length="15" not-null="true" />
</property>
<property name="firstName" type="string">
<column name="first_name" length="20" />
</property>
<property name="lastName" type="string">
<column name="last_name" length="25" />
</property>
<set name="roleses" table="users_roles" inverse="true" lazy="false" fetch="select" cascade="all">
<key>
<column name="user_name" length="15" not-null="true" />
</key>
<many-to-many entity-name="com.antoiovi.jobprograms.entity.Roles">
<column name="role_name" length="20" not-null="true" />
</many-to-many>
</set>
<set name="jobprograms" table="jobprogram" inverse="true" lazy="false" fetch="select" cascade="all">
<key>
<column name="users_idusers" not-null="true" />
</key>
<one-to-many class="com.antoiovi.jobprograms.entity.Jobprogram" />
</set>
</class>
I have made some modification as you can see above (auto-import=true, lazy=false), cause i have the error message
rg.hibernate.MappingException: An association from the table users_roles refers to an unmapped class: com.antoiovi.jobprograms.entity.Roles
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1824)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1756)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
The config file is
<hibernate-configuration>
org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/jobprograms?zeroDateTimeBehavior=convertToNull
jobprograms_ad
xxxxx
thread
true
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
true
the classes have refernce like this :
'#ManyToMany(fetch=FetchType.LAZY, mappedBy="roleses")
public Set<Users> getUserses() {
return this.userses;
} '
#ManyToMany(fetch=FetchType.EAGER)
#JoinTable(name="users_roles", catalog="jobprograms", joinColumns = {
#JoinColumn(name="user_name", nullable=false, updatable=false) }, inverseJoinColumns = {
#JoinColumn(name="role_name", nullable=false, updatable=false) })
public Set<Roles> getRoleses() {
return this.roleses;
}
wHEN RUNNING I HAVE THE ERROR org.hibernate.exception.SQLGrammarException, and in fact the entiti is loaded, but the set<> not..,
When testing the error is
org.hibernate.MappingException: An association from the table users_roles refers to an unmapped class: com.antoiovi.jobprograms.entity.Roles, and the HSQL ar not executed.
I tried to look in others post but i couldn't find an answer. Can anybody help me?
In config file the entities are all declared;
I think yhe promlem is in the mapping :
e <set name="roleses" table="users_roles" inverse="false" lazy="true" fetch="select" >
<key >
<column name="user_name" length="15" not-null="true" />
</key>
<many-to-many entity-name="test.Roles" property-ref="rolesName">
<column name="role_name" length="20" not-null="true" />
</many-to-many>
</set>
<set name="jobprograms" table="jobprogram" inverse="true" lazy="true" fetch="select">
<key>
<column name="users_idusers" not-null="true" />
</key>
<one-to-many class="test.Jobprogram" />
</set>
and
enter <class name="test.Roles" table="roles" catalog="jobprograms">
<id name="rolesName" type="string">
<column name="roles_name" length="20" />
<generator class="assigned" />
</id>
<set name="userses" table="users_roles" inverse="true" lazy="true" fetch="select">
<key property-ref="rolesName">
<column name="role_name" length="20" not-null="true" />
</key>
<many-to-many entity-name="test.Users" property-ref="userName">
<column name="user_name" length="15" not-null="true" />
</many-to-many>
</set>
</class>
....
this error
org.hibernate.MappingException: An association from the table users_roles refers to an unmapped class: com.antoiovi.jobprograms.entity.Roles
comes when hibernate configuration dont know about entity(Roles) mapping , either you have missed introducing enitity Roles to the hibernate configuration or you have not used correct name , please check configuraiton file and included com.antoiovi.jobprograms.entity.Roles as resourse
I think to have resolved the problem. Actually the proble are unmapped entities. But this happens because the database is not 'well formed'. Indeed the database ha 3 table users, users_roles, roles; the table roles has only one column as primarykey (varchar),and the table user_roles refers to it from its column user_roles; when forword enginering the ide (in the case netbeans) creates only 2 entities: 'Users', and 'Roles'; So i have re-designed the schema putting some virtual primary key, and creating unique constaint instead. Now i have 3 entities : Users,UserRoles, and Roles.

Hibernate many to many Object values are void

I'm trying to get a many to many mapped Object from a ValueObject.
See the structure below:
Owner
Owner_link
Dog
See the mapping:
Owner.hbm.xml
<set name="dogs" table="schema.owner_link" inverse="true" lazy="true" fetch="select" >
<key foreign-key="none">
<column name="fk_dog_id" not-null="true"/>
</key>
<many-to-many entity-name="de.neo7even.database.pojo.DOG">
<column name="id" not-null="true" />
</many-to-many>
</set>
Dog.hbm.xml
<set name="owners" table="schema.owner_link" inverse="true" lazy="true" fetch="select">
<key foreign-key="none">
<column name="fk_dog_id" not-null="true" />
</key>
<many-to-many entity-name="de.neo7even.database.pojo.Owner">
<column name="fk_owner_id" not-null="true" />
</many-to-many>
</set>
The code assigns null to my Dog variable.
Why fails the code? I'll provide more information if required.

Categories

Resources