Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6033965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:33:37+00:00 2026-05-23T05:33:37+00:00

I have the following class: @Entity @Table(name=Player) public class Player { private String id;

  • 0

I have the following class:

@Entity
@Table(name="Player")
public class Player {

    private String id;
    private Set<String> nicknames = new HashSet<String>();

        @ElementCollection(fetch=FetchType.LAZY)
    @CollectionTable(name="Nicknames", joinColumns=@JoinColumn(name="user_id"))
    @Column(name="nickname")
    public Set<String> getNicknames() {
        return nicknames;
    }
}

My DaoServiceImpl looks like that:

@Repository("daoService")
public class DaoServiceImpl implements DaoService {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public String addPlayer(Player player){
        Session session = sessionFactory.getCurrentSession();
        return (String) session.save(player);
    }

    @Override
    public Player getPlayer(String id){
        Session session = sessionFactory.getCurrentSession();
        return (Player)session.load(Player.class, id);
    }

    @Override
    public void updatePlayer(Player player){
        Session session = sessionFactory.getCurrentSession();
        session.update(player);
    }
}

My Transaction Manager looks like that:

<aop:config>
    <aop:pointcut id="daoServicePoint" 
            expression="execution(* com.vanilla.daoService.DaoService.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="daoServicePoint"/>
  </aop:config>

  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="add*" propagation="REQUIRED"/>
       <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
  </tx:advice>

In my main function I’m doing the following:

public void run(){
Player player = new Player();
player.addNickName("Nick1");
player.addNickName("Nick2");
player.setFirstName("Danny");
player.setLastName("Lesnik");
String playerId = daoService.addPlayer(player);
player = daoService.getPlayer(playerId);
player.addNickName("Nick4");
player.removeNickName("Nick2");
daoService.updatePlayer(player);
}

I’m getting the following Exception on player.addNickName

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
    at com.vanilla.objects.Player_$$_javassist_0.addNickName(Player_$$_javassist_0.java)
    at com.vanilla.main.Main.run(Main.java:27)
    at com.vanilla.main.Main.main(Main.java:36)

If it was Web Application, I would use OpenSessionIviewFilter.
How should I solve it Spring Hibernate Java Console Application?

I know that I can use join HQL query or make it Eager, but I would like to leave it lazy for self learning purposes 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:33:38+00:00Added an answer on May 23, 2026 at 5:33 am

    String playerId = daoService.addPlayer(player);
    in above code you save the entity and at that current session closed. After that you try to load players using same session. But there is no open session.You have to open new session at getPlayer method,

    Your code should change as bellow

    public Player getPlayer(String id){
    Session session = sessionFactory.openSession();
    return (Player)session.load(Player.class, id);
    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model classes: @Entity @Table(name = title) public final class Title
I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package
I have the following inner class: @Entity @Table(name = SATTET0) public class SATTET0Key {
I have the following entity public class DocumentHistory { public string Name { get;
I have the following property defined: @Entity @Table(name = person) public class Person extends
I have the 2 following entities: @Entity @Table(name = brand) public class Brand {
I have the following entity (getters and setters ommited)... @Entity @Table(name = TBL_PROJECT_RUN) public
I have this code @Entity @Table(name = picture) public class Picture implements Serializable {
I have the following Entity: @Entity @Table @NamedQuery(name = Constants.FINDALLFINDERNAME, query = Constants.FINDALLQUERY) public
I have the following tables Entity id,name,categoryid 21,Blah,1 EntityCategory (Enum table) id, name 1,New

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.