Hibernate many to many Object values are void - java

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.

Related

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

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.

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.

Hibernate manytomany cascading

I got two entities, "project" and "employee", a project cointains 0, 1 or many employees and an employee can participate on 0, 1 or many projects.
So its a normal many to many relationship and a third table is required...lets call it "ProjectEmployee".
When i delete a project, the project itself should be deleted and the proper entry/entries in ProjectEmployee. But the employees should still exist.
Same for employee...so it should be possible to have many entries in project and in employee but not a single one in ProjectEmployee.
I hope its clear what I would like to have.
The problem is now that I donĀ“t know how the correct mapping(XML) should look like.
At the moment it is as follows:
Project:
<set name="employees" table="ProjectEmployee" cascade="save-update">
<key column="projectId" />
<many-to-many class="Employee" column="employeeId" />
</set>
<set name="modules" cascade="save-update,delete-orphan" fetch="select" lazy="true" inverse="true">
<key column="projectId" />
<one-to-many class="Modul" />
</set>
</class>
Employee
<class name="Employee">
<id name="id" type="long">
<generator class="sequence"></generator>
</id>
<property name="firstName" type="string">
<column name="firstName" />
</property>
<property name="lastName" type="string">
<column name="lastName" />
</property>
<property name="dateOfBirth" type="date">
<column name="dateOfBirth" />
</property>
<set name="logbookEntries" inverse="true" cascade="save-update,delete-orphan"
fetch="select" lazy="true">
<key column="employeeId" />
<one-to-many class="LogbookEntry" />
</set>
<set name="issues" inverse="true" cascade="save-update,delete-orphan"
fetch="select" lazy="true">
<key column="employeeId" />
<one-to-many class="Issue" />
</set>
<one-to-one name="address" class="Address" cascade="save-update, delete" />
<set name="projects" table="ProjectEmployee" inverse="true"
cascade="save-update">
<key column="employeeId" />
<many-to-many class="Project" column="projectId"/>
</set>
<joined-subclass name="PermanentEmployee">
<key column="id" />
<property name="salary" />
</joined-subclass>
<joined-subclass name="TemporaryEmployee">
<key column="id" />
<property name="hourlyRate"></property>
<property name="renter"></property>
<property name="startDate"></property>
<property name="endDate"></property>
</joined-subclass>
</class>
At the moment, I get an Exception when I would like to delete an employee.
org.hibernate.exception.ConstraintViolationException
Thats because I didnt configure "cascade:delete"!
I hope you can help me.

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.

Categories

Resources