Hibernate saveOrUpdate() doesn't seem to cascade creation and saving - java

I'm new to Hibernate and am trying to save a "UserState" with a list of "WorkspaceState"s. UserState objects are keyed by a username that is set, the WorkspaceStates are set by UUID scheme. My issue is that if I have a UserState with one WorkspaceState in it, the UserState gets saved but the WorkspaceState does not.
Here are the Hibernate mapping
<hibernate-mapping>
<class name="UserState" table="USERSTATE">
<id name="owner" type="java.lang.String">
<column name="OWNER" />
<generator class="assigned" />
</id>
<list name="workspaces" inverse="false" cascade="all" table="WORKSPACESTATE" lazy="true">
<key>
<column name="UID" />
</key>
<list-index></list-index>
<one-to-many class="WorkspaceState" />
</list>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="WorkspaceState" table="WORKSPACESTATE">
<id name="uid" type="java.lang.String">
<column name="UID" />
<generator class="uuid" />
</id>
<property name="owner" type="java.lang.String">
<column name="OWNER" />
</property>
</class>
</hibernate-mapping>
I have a UserState object with one WorkspaceState in it. When I do a session.saveOrUpdate(userst), I see that hibernate has already removed the WorkspaceState from my userst object. Then the commit saves it to the DB without the workspacestate in it.
In truth the WorkspaceState themselves have lists, but I suspect whatever I'm doing wrong continues onward.
Thanks
Edit - how it's committed. HibernateUtil is as it appears in the standard hibernate document examples:
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
String username = (String) session.merge(state);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
return false;
} finally {
session.close();
}

you need to change cascade reference to save-update and remove inverse attribute from workspace list mapping

Related

Hibernate lazy loading not working many-to-one XML

I'm trying to lazily load a collection in Hibernate. From what I've read this is the best practice way to do this:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Hibernate.initialize(this.truckReviews);
session.getTransaction().commit();
session.close();
return this.truckReview;
However, when I run this unit test, the assertion fails:
Truck realTruck = Truck.getTruckByID(1);
TruckReview realFakeReview = new TruckReview();
realFakeReview.setTruck(realTruck);
realFakeReview.setUser(IntegrationTestResources.getTestUser());
realFakeReview.setReviewDate(new Date());
realFakeReview.setReviewStars(5);
realFakeReview.setReviewText("fake review");
realFakeReview.save();
assertTrue(realTruck.loadReviews().contains(realFakeReview));
realFakeReview.delete();
Needless to say, I'm a bit confused since other StackOverflow threads say this is the way to do it properly. I feel like I must be missing something very obvious but I'm still new to Hibernate and have no idea what.
Here's the mapping file for this class:
<hibernate-mapping>
<class name="edu.temple.tutrucks.Truck" table="truck" catalog="TUTrucks" optimistic-lock="version">
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="truckName" type="string">
<column name="truck_name" not-null="true" />
</property>
<property name="latitude" type="double">
<column name="latitude" precision="22" scale="0" not-null="true" />
</property>
<property name="longitude" type="double">
<column name="longitude" precision="22" scale="0" not-null="true" />
</property>
<property name="openingTime" type="imm_time">
<column name="opening_time" not-null="false"></column>
</property>
<property name="closingTime" type="imm_time">
<column name="closing_time" not-null="false"></column>
</property>
<property name="avatar" type="string">
<column name="avatar"></column>
</property>
<list name="truckReviews" table="truck_review" inverse="true" lazy="true" fetch="select">
<key>
<column name="truck_id" />
</key>
<list-index column="review_stars" />
<one-to-many class="edu.temple.tutrucks.TruckReview" />
</list>
<list name="menus" table="menu" inverse="true" lazy="false" fetch="select">
<key>
<column name="truck_id" />
</key>
<list-index column="id" />
<one-to-many class="edu.temple.tutrucks.Menu" />
</list>
<set name="tags" table="tag_truck_map" inverse="true" lazy="true" fetch="select">
<key>
<column name="truck_id" />
</key>
<many-to-many column="tag_id" class="edu.temple.tutrucks.Tag"></many-to-many>
</set>
</class>
</hibernate-mapping>
Thanks in advance.
UPDATE: Still having issues, I've changed the loadReviews method to this
#Override
public Truck loadReviews() { // lazy loading needs to be fixed
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Truck retval = (Truck) session.get(Truck.class, this.getId());
Hibernate.initialize(retail.getTruckReviews());
session.getTransaction().commit();
session.close();
retval.truckReviews.size();
return retval;
}
And the test now looks like this
#Test
public void testloadReviews() {
Truck realTruck = Truck.getTruckByID(1, false, false);
TruckReview realFakeReview = new TruckReview();
realFakeReview.setTruck(realTruck);
realFakeReview.setUser(IntegrationTestResources.getTestUser());
realFakeReview.setReviewDate(new Date());
realFakeReview.setReviewStars(5);
realFakeReview.setReviewText("fake review");
realFakeReview.save();
assertTrue(realTruck.loadReviews().getTruckReviews().contains(realFakeReview));
realFakeReview.delete();
}
The problem is that you can't to use realTruck form one session to do Hibernate.initialize(this.truckReviews) using other session
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
// you need to reload persistent with
// realTruck = session.get(), realTruck = session.load() here
Hibernate.initialize(this.getTruckReviews());
You need to use a getter not a property, because of Hibernate need to have a proxy. A proxy is returned by a getter
Hibernate.initialize(this.getTruckReviews());

How to get only parent without children in Hibernate and without lazy exception?

How to get only parent without children in Hibernate and without lazy exception, when you trying to access chidlren filed after closing session?
I mean, a kind of pure parent obtainingm but I beed to be sure, that somebody will not obtain them with getter or will not catch a lazy exception.
<class name="com.electronic.commerce.models.Category" table="Category">
<id name="id" type="long" access="property">
<column name="categoryId" length="20"/>
<generator class="native"/>
</id>
<property name="name" column="categoryName" type="string"/>
<property name="parentId" column="categoryParentId" type="long"/>
<bag name="children" table="Category" inverse="true" lazy="true" fetch="select">
<key>
<column name="categoryParentId" not-null="true"/>
</key>
<one-to-many class="com.electronic.commerce.models.Category"/>
</bag>
</class>
Mixed solution
Xml:
<prop key="hibernate.enable_lazy_load_no_trans">false</prop>
Java:
DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
defaultTransactionDefinition.setPropagationBehavior(transactionDefinition);
transactionManager.getTransaction(defaultTransactionDefinition);

lazy="false" not working in hibernate 4.3.x

I am trying to test the functioning of lazy loading in hibernate with the following code.
Query query = session.createQuery("From Publishers as publisher where publisher.publisherName <= :name");
query.setString("name", "penguin");
Publishers publisher = (Publishers)query.iterate().next();
Set<Books> booksByPublisher = new HashSet<>();
booksByPublisher = publisher.getBooks();
session.close();
System.out.println(publisher.getPublisherName());
for(Books book : booksByPublisher) {
System.out.println(book);
}
mapping for publishers and books classes are as following, respectively:
<class name="newapplication.domain.Publishers" table="PUBLISHERS" >
<id name="publisherId" type="integer" column="PUBLISHER_ID"/>
<property name="publisherName" type="string" column="PUBLISHER_NAME"/>
<set name="books" table="BOOKS_PUBLISHERS" inverse="true" fetch="join" lazy="false">
<key column="publisher"/>
<many-to-many class="newapplication.domain.Books" column="book" />
</set>
</class>
<class name="newapplication.domain.Books" table="BOOKS" >
<id name="bookId" type="integer" column="BOOK_ID">
<generator class="sequence">
<param name="sequence">BOOKIDSEQUENCE</param>
</generator>
</id>
<property name="bookName" type="string" column="NAME"/>
<many-to-one name="author" class="newapplication.domain.Authors" column="AUTHOR"/>
<many-to-one name="cover" class="newapplication.domain.Covers" column="COVER"/>
<set name="publishers" table="BOOKS_PUBLISHERS" lazy="false">
<key column="BOOK"/>
<many-to-many class="newapplication.domain.Publishers" column="PUBLISHER"/>
</set>
</class>
Despite having set lazy="false" loading in the publisher class i get the following error.
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
What is wrong here? Shouldn't the Set be loaded as soon as the database is hit for the publishers' object?
Your many-to-one associations in Books are lazy; that's where the exception comes from.
By default, all associations are lazy when using Hibernate native mappings, as opposed to JPA standard (when using JPA-style mappings) where to-one associations are eager by default.

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.

Hibernate parent/child relationship. Why is object saved twice?

I am looking into Hibernate's parent/child relationships.
I have 3 entities Employee Customers and Orders.
Their relationship is
Employee 1 <-> N Orders
Customer 1 <-> N Orders
I want to be able to save/update/delete Customer objects and Employees and Orders but I want to use some interface so that the calling code does not deal with any Hibernate or JPA stuff.
E.g. I tried something like the following:
class Utils{
public static void saveObject(Object o){
logger.debug(o.toString());
Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.save(o);
tx.commit();
session.close();
}
}
and in the calling code I do:
Employee employee = new Employee();
//set data on employee
Customer customer = new Customer();
//set data on customer
Order order = new Order();
//set data on order
employee.addOrder(order);//this adds order to its list, order gets a reference of employee as parent
customer.addOrder(order);//this adds order to its list, order gets a reference of customer as parent
Utils.saveObject(customer);
Utils.saveObject(employee);
Now I noticed that with this code, 2 records of employee are created instead of 1.
If I only do:
Utils.saveObject(customer);
Only 1 (correctly) record is created.
Why does this happen?
Does this happens because the same Order object is saved by both Customer and Employee? And the cascade="all" makes this side-effect?
Now if I do not use the DBUtils method and do directly:
Session session = DBUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.save(employee);
session.save(customer);
tx.commit();
session.close();
Again, it works as expected. I.e. only 1 employee record is created.
What am I doing something wrong here?
UPDATE:
Hibernate mappings:
<hibernate-mapping>
<class name="database.entities.Associate" table="EMPLOYEE">
<id name="assosiateId" type="java.lang.Long">
<column name="EMPLOYEEID" />
<generator class="identity" />
</id>
<property name="firstName" type="java.lang.String" not-null="true">
<column name="FIRSTNAME" />
</property>
<property name="lastName" type="java.lang.String" not-null="true">
<column name="LASTNAME" />
</property>
<property name="userName" type="java.lang.String" not-null="true">
<column name="USERNAME" />
</property>
<property name="password" type="java.lang.String" not-null="true">
<column name="PASSWORD" />
</property>
<set name="orders" table="ORDERS" inverse="true" cascade="all" lazy="true">
<key>
<column name="EMPLOYEEID" />
</key>
<one-to-many class="database.entities.Order" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="database.entities.Customer" table="CUSTOMER">
<id name="customerId" type="java.lang.Long">
<column name="CUSTOMERID" />
<generator class="identity" />
</id>
<property name="customerName" type="java.lang.String">
<column name="CUSTOMERNAME" />
</property>
<set name="orders" table="ORDERS" inverse="true" cascade="all" lazy="true">
<key>
<column name="CUSTOMERID" />
</key>
<one-to-many class="database.entities.Order" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="database.entities.Order" table="ORDERS">
<id name="orderId" type="java.lang.Long">
<column name="ORDERID" />
<generator class="identity" />
</id>
<property name="orderDate" type="java.util.Date">
<column name="ORDERDATE" />
</property>
<property name="quantity" type="java.lang.Integer">
<column name="QUANTITY" />
</property>
<property name="quantityMargin" type="java.lang.Long">
<column name="QUANTITYMARGIN" />
</property>
<property name="port" type="java.lang.String">
<column name="PORT" />
</property>
<property name="orderState" type="java.lang.String">
<column name="ORDERSTATE" />
</property>
<many-to-one name="customer" class="database.entities.Customer" cascade="all" fetch="join">
<column name="CUSTOMERID" />
</many-to-one>
<many-to-one name="associate" column="EMPLOYEEID" class="database.entities.Employee" cascade="all" fetch="join">
</many-to-one>
</class>
</hibernate-mapping>
UDATE 2:
class Employee{
//Various members
Set<Order> orders = new HashSet<Order>();
public void addOrder(Order order){
order.setEmployee(this);
orders.add(order);
}
}
Also:
class Customer{
//Various members
Set<Order> orders = new HashSet<Order>();
public void addOrder(Order order){
order.setCustomer(this);
orders.add(order);
}
}
It seems to me that in the DBUtils code, you are wrapping both saves in an outer transaction, whereas in the Utils code, you have them in two completely separate transactions without an outer one.
Depending on your cascading, Hibernate has to figure out which objects need to be saved. When you run the Utils code, with two separate transactions, it will save the Order first. Since you're cascading all, this means it will save the Order first, then the Customer. However, you've created the SAME Order for both the Customer and Employee. Therefore, the Employee also gets saved in the first transaction (due to cascading). The second transaction, since it is separate, will not be aware of this and save another Employee.
On the other hand, if you wrap them in an outer transaction, Hibernate can figure out the identities of all the objects and save them properly.
try saving only Order instead of saving Employee and Customer separately. With your existing cascade=all setting both parent object will get saved without creating any duplicates.

Categories

Resources