Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Arthur Grohe
No Hibernate Session bound to thread when trying to acces Modelclass
November 5, 2012 8:44 AM
Answer

Arthur Grohe

Rank: Junior Member

Posts: 49

Join Date: November 5, 2012

Recent Posts

Hi guys,

I have the following setup:
+ Plugin Portlet Project with JSF 2.x and Liferay Faces Alloy
+ Liferay SDK 6.1.1, Liferay Portal 6.1 CE (Tomcat 7)

I get this exception when trying to access a model class created by the service builder from within a javaBean.

My question is: What is the recommended way to access instances of Model-Classes built by the service builder with an jsf architecture?

view.xhtml
 1<f:view
 2    xmlns="http://www.w3.org/1999/xhtml"
 3    xmlns:aui="http://liferay.com/faces/aui"
 4    xmlns:c="http://java.sun.com/jsp/jstl/core"
 5    xmlns:f="http://java.sun.com/jsf/core"
 6    xmlns:h="http://java.sun.com/jsf/html"
 7    xmlns:ui="http://java.sun.com/jsf/facelets"
 8>
 9    <h:head />
10   
11    <h:body>
12        <h:outputText value="#{i18n['my-greeting-hello-world']}" />
13        Book name: <h:outputLabel value="#{bookBean.book.userName}" /><!-- this causes the error! -->
14    </h:body>
15</f:view>


BookBean.java
 1import com.liferay.portal.kernel.exception.SystemException;
 2import com.sample.portlet.library.NoSuchBookException;
 3import com.sample.portlet.library.model.Book;
 4import com.sample.portlet.library.service.persistence.BookPersistence;
 5import com.sample.portlet.library.service.persistence.BookUtil;
 6
 7
 8@ManagedBean
 9public class BookBean {
10
11    public Book getBook() throws SystemException{
12        Book b = null;
13        BookPersistence bu = BookUtil.getPersistence();
14        try {
15            b = bu.findByPrimaryKey(1234);
16        } catch (NoSuchBookException e) {
17            e.printStackTrace();
18        }
19   
20        if(b == null){
21            b = bu.create(1234);
22            bu.updateImpl(b, true);
23        }
24       
25        return b;
26    }
27}


service.xml
 1<service-builder package-path="com.sample.portlet.library">
 2    <namespace>Library</namespace>
 3   
 4    <entity name="Book" local-service="true" remote-service="true"> 
 5    <!-- PK fields -->
 6        <column name="bookId" type="long" primary="true" /> 
 7   
 8    <!-- Group instance -->
 9        <column name="groupId" type="long" /> 
10   
11    <!-- Audit fields -->
12        <column name="companyId" type="long" />
13        <column name="userId" type="long" />
14        <column name="userName" type="String" />
15        <column name="createDate" type="Date" />
16        <column name="modifiedDate" type="Date" /> 
17   
18    <!-- Other fields -->
19        <column name="title" type="String" />
20        <column name="authorId" type="long"></column>
21    </entity>
22    <entity name="Author">
23        <column name="authorId" type="double" primary="true"></column>
24        <column name="name" type="String" primary="false"></column>
25    </entity>
26   
27</service-builder>
David H Nebinger
RE: No Hibernate Session bound to thread when trying to acces Modelclass
November 5, 2012 9:58 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7027

Join Date: September 1, 2006

Recent Posts

You should never access BookPersistence directly.

Instead you should be using BookLocalServiceUtil methods to access entities.
Arthur Grohe
RE: No Hibernate Session bound to thread when trying to acces Modelclass
November 6, 2012 12:39 AM
Answer

Arthur Grohe

Rank: Junior Member

Posts: 49

Join Date: November 5, 2012

Recent Posts

Thanks for the superfast answer David, and it works perfectly!

I find it very confusing that the service builder creates about 30 java classes for 1 Model emoticon
And the other even more confusing thing is, I didnt find a good documentation that told me
what classes I should use, and what the several classes do.

And the last confusing thingy is that the magically correct class MyClassLocalServiceUtil is NOT SHOWN
in the Eclipse source packages.

I only shows some of the generated service-builder classes, like *LocalServiceImpl.java, *ServiceImpl.java, *PersistenceImpl.java .....
so I have to scroll down in the Eclipse-package-explorer and look into docroot/WEB-INF/service/my/package/name/service/
And in this folder there is *LocalServiceUtil and *ServiceUtil.

Thanks again David, you saved the day!


For the ones who are interested, here is my modified bean:

BookBean.java - getBook()
 1    public Book getBook() throws SystemException{
 2        Book b = null;
 3        try {
 4            b = BookLocalServiceUtil.fetchBook(1234);
 5        }catch(SystemException e){
 6            e.printStackTrace();
 7        }
 8   
 9        if(b == null){
10            b = BookLocalServiceUtil.createBook(1234);
11            BookLocalServiceUtil.updateBook(b, true);
12        }
13        b.setUserName("Peter Klause");
14       
15        return b;
16    }
Oliver Bayer
RE: No Hibernate Session bound to thread when trying to acces Modelclass
November 6, 2012 12:52 AM
Answer

Oliver Bayer

Rank: Liferay Master

Posts: 875

Join Date: February 18, 2009

Recent Posts

Hi Arthur,

the MyClassLocalServiceUtil isn't shown in the eclipse source path because you should never edit this file. If you need new methods change MyClassLocalServiceImpl and then re-run the service builder so the method gets added to MyClassLocalServiceUtil. After that the MyClassLocalServiceUtil class is put into the jar file of your service. The book "Liferay in Action" can be of great help in learning the programming internals.

HTH Oli
Arthur Grohe
RE: No Hibernate Session bound to thread when trying to acces Modelclass
November 6, 2012 1:36 AM
Answer

Arthur Grohe

Rank: Junior Member

Posts: 49

Join Date: November 5, 2012

Recent Posts

Hello Oliver,

thank you for the explanation.
I find it a good idea to only show the classes in source path that the developer may modify.

But unfortunately I found some classes which are shown under the source path, generated by the service-builder, which should not be modified,
that are (assuming my Models name is "Book"):

com.sample.portlet.library.service.persistence
  • BookPersistenceImpl


com.sample.portlet.library.service.base
  • BookServiceBaseImpl
  • BookLocalServiceBaseImpl
  • BookLocalServiceClpInvoker (?)
  • BookServiceClpInvoker (?)


com.sample.portlet.library.model.impl
  • BookModelImpl
  • BookCacheModel (?)
  • BookBaseImpl


So what the service-builder builds is kind of inconsistent IMHO. It should either put all classes in the source path or really just the ones that may be modified by the developer.
David H Nebinger
RE: No Hibernate Session bound to thread when trying to acces Modelclass
November 6, 2012 5:33 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7027

Join Date: September 1, 2006

Recent Posts

It's not structured that way, and is just a learning curve you'll need to get over...

SB creates two jars - a service jar containing methods/interfaces your class should use and the internal implementation jar your plugins never use.

In the implementation, the classes are all cleanly separated by function into their own package paths. The model package contains the model information, the service package is broken down into base (for base class info), and other sub packages to separate their functionality.

Each class generated includes a comment about what you should not modify (i.e. the base classes) and indicate where your changes should go. Basically for service changes you'll always be in the XxxLocalServiceImpl or XxxServiceImpl classes (for remote APIs).

The first SB implementation you do will be the hardest because you need to get your head around some basic yet important SB concepts. Once you get through that first implementation, it will be easier for future implementations...
Arthur Grohe
RE: No Hibernate Session bound to thread when trying to acces Modelclass
November 6, 2012 5:59 AM
Answer

Arthur Grohe

Rank: Junior Member

Posts: 49

Join Date: November 5, 2012

Recent Posts

Thanks again David,

maybe we talked about different things, maybe not...

I hope this screenshot makes everything clear: (As you can see the LocalServiceUtil is not shown in the source path)
David H Nebinger
RE: No Hibernate Session bound to thread when trying to acces Modelclass
November 6, 2012 11:01 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7027

Join Date: September 1, 2006

Recent Posts

Right. You can make changes to the XxxLocalServiceImpl classes. The classes in the docroot/WEB-INF/service folder are generated and maintained by ServiceBuilder and you should never, ever, be editing these files (they are regenerated when you build services).