Hi,

I've read about the Hibernate entity persistence strategy on
http://tapestry.apache.org/hibernate-user-guide.html
As I do not want to be Hibernate-specific, I searched for an equivalent for JPA 
and found it not in the guide, but searching in Eclipse:
JpaSymbols.ENTITY_SESSION_STATE_PERSISTENCE_STRATEGY_ENABLED
This seems to enable the entity persistence strategy for session state objects.
Applying this doesn't make the error go away, though.

I'm gonna include some code excerpts here, maybe I've done something horribly 
wrong.

In AppModule:
    public static void contributeApplicationDefaults(
            MappedConfiguration<String, Object> configuration)
    {
        
configuration.add(JpaSymbols.ENTITY_SESSION_STATE_PERSISTENCE_STRATEGY_ENABLED, 
"true");
    }

My session state object:

public class AppSession {
       
       private User loggedInUser;
       
       private Date loginDate;
       
       public AppSession(User loggedInUser) throws ClientProtocolException, 
IOException {
             this.loggedInUser = loggedInUser;
             this.loginDate = new Date();
       }

       public User getLoggedInUser() {
             return loggedInUser;
       }

       public Date getLoginDate() {
             return loginDate;
       }
}

The user entity:
@Entity
@NamedQueries ({
       @NamedQuery(name=User.FIND_BY_USERNAME, query="SELECT u FROM User u 
WHERE u.username = ?1")
})
public class User {
       
       public static final String FIND_BY_USERNAME = "User.findByUserName";

       @Id
       @GeneratedValue(strategy=GenerationType.IDENTITY)
       @NonVisual
       private long id;

       @Validate(value="required")
       @Column(nullable=false,unique=true)
       private String username;
       
       @Lob
       @Column(nullable=false)
       private byte[] password;
       
       @Column(nullable=false)
       @Validate(value="email")
       private String email;
       
       @ManyToMany
       @JoinTable
       private Set<UserGroup> groups;
       
       public User() {
             groups = new HashSet<UserGroup>();
       }

       public Set<UserGroup> getGroups() {
             return Collections.unmodifiableSet(groups);
       }
       

Creating the session in my layout component (excerpt):
       @SessionState
       AppSession session;

       public Object onActionFromLogout() {
             session = null;
             return Index.class;
       }

       void onValidateFromLoginForm() {
              authenticatedUser = appAuthenticator.authenticateUser(userName, 
password);
}
                
       Object onSuccessFromLoginForm() {
             session = new AppSession(authenticatedUser);
       }

Authenticating a user:
       public User authenticateUser(String username, String password) throws 
NoExistingUserException, CryptoUtilException {
       User user = userDAO.findUserByName(username);
       return CryptoUtil.checkHash(CryptoUtil.calculateHash(password), 
user.getPassword()) ? user : null;
                
Using the session state object in a page:
public class Hauptseite {

       @SessionState
       private AppSession session;

       void setupRender() {
             for (UserGroup group : session.getLoggedInUser().getGroups()) {
                    System.out.println(group.toString());
             }

       }

Any pointers? Do I have to use the user as a direct SessionState object? Can't 
I use a wrapper class?

Regards,
Daniel P.

Von: Poggenpohl, Daniel 
Gesendet: Mittwoch, 18. März 2015 11:21
An: users@tapestry.apache.org
Betreff: Session Storage with Tapestry

Hello again,

a slightly different topic as the last but with the same "undertones":

I need a session storage where I store the currently logged on user. Reading 
about it in the documentation, they recommend using @SessionState because my 
user is a complex object, also containing lists of other entities.

My user also is an entity in a database. So, when a user logs in, the 
appropriate entity is retrieved from the database, an "AppSession" object is 
created containing, among e.g. the time of login, the user object. Is this the 
right way to do it? Or should I only store the ID in the session?

Now when the SessionState object is created, it can be used in any other page 
or component using the same SessionState annotation and the same type. Does it 
need to be the same name
I'd say it doesn't, as I've not read otherwise.

My user contains lazy collections of other entities. Every page of my app 
contains the layout component which provides the login area and serves to 
create the session object for the app, retrieving the user entity in the 
process.
A page containing the layout component itself contains another component where 
these lazy collections are needed. The user logs in, the SessionState object is 
created, the user entity is stored inside the object. The page is requested 
again, and the component is initialized/rendered. The component contains a 
reference to the SessionState object. Inside the component, a tree should 
display objects of the lazy collection.
To do this, a service receives the user. The service tries to access the lazy 
collection, but fails with a 
"failed to lazily initialize a collection of role:".

What I gather from this is, services don't operate within transactions?

UPDATE: I tried to access the lazy collection from the component itself, but 
the error was the same.
Even from the page, the error still was present.

This leads me to the point that I may be doing something wrong using an entity 
in a session storage?

Regards,
Daniel Poggenpohl

-----Ursprüngliche Nachricht-----
Von: Poggenpohl, Daniel [mailto:daniel.poggenp...@isst.fraunhofer.de] 
Gesendet: Mittwoch, 18. März 2015 11:21
An: users@tapestry.apache.org
Betreff: Session Storage with Tapestry

Hello again,

a slightly different topic as the last but with the same "undertones":

I need a session storage where I store the currently logged on user. Reading 
about it in the documentation, they recommend using @SessionState because my 
user is a complex object, also containing lists of other entities.

My user also is an entity in a database. So, when a user logs in, the 
appropriate entity is retrieved from the database, an "AppSession" object is 
created containing, among e.g. the time of login, the user object. Is this the 
right way to do it? Or should I only store the ID in the session?

Now when the SessionState object is created, it can be used in any other page 
or component using the same SessionState annotation and the same type. Does it 
need to be the same name I'd say it doesn't, as I've not read otherwise.

My user contains lazy collections of other entities. Every page of my app 
contains the layout component which provides the login area and serves to 
create the session object for the app, retrieving the user entity in the 
process.
A page containing the layout component itself contains another component where 
these lazy collections are needed. The user logs in, the SessionState object is 
created, the user entity is stored inside the object. The page is requested 
again, and the component is initialized/rendered. The component contains a 
reference to the SessionState object. Inside the component, a tree should 
display objects of the lazy collection.
To do this, a service receives the user. The service tries to access the lazy 
collection, but fails with a "failed to lazily initialize a collection of 
role:".

What I gather from this is, services don't operate within transactions?

UPDATE: I tried to access the lazy collection from the component itself, but 
the error was the same.
Even from the page, the error still was present.

This leads me to the point that I may be doing something wrong using an entity 
in a session storage?

Regards,
Daniel Poggenpohl

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to