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 9021837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:21:06+00:00 2026-06-16T05:21:06+00:00

I have: @Entity public class EmailAndName { … } and @Entity public class MessageDetails

  • 0

I have:

@Entity
public class EmailAndName {
...
}

and

@Entity
public class MessageDetails {
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "MessageDetails_to")
    public Set<EmailAndName> getTo() {
        return to;
    }
}

when I

public static void main(String []a)
{
    StatelessSession sess = HibernateUtils.getSessionFactory().openStatelessSession();
    sess.beginTransaction();
    MessageDetails messageDetails = new MessageDetails();
    messageDetails.setTo(new HashSet<EmailAndName>());
    EmailAndName emailAndName = (EmailAndName) sess.get(EmailAndName.class, 1L);
    if (emailAndName == null ) throw new RuntimeException();
    messageDetails.getTo().add(emailAndName);
    sess.insert(messageDetails);
    sess.getTransaction().commit();
}

MessageDetails_to table is not populated. What should I do? I don’t want to write native queries. Thank you.

  • 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-06-16T05:21:07+00:00Added an answer on June 16, 2026 at 5:21 am

    You havent read through hibernate-doc for stateless session

    It clearly states that:

    Operations performed using a stateless session never cascade to associated instances. Collections are ignored by a stateless session.
    

    And you are trying to add a Set<EmailAndName>. A stateless session is a lower-level abstraction, much closer to the underlying JDBC. SO if you really want make your code work and populate MessageDetails_to.. You need to go for Session. You will need to define the equals and hashCode methods for your POJO’s 🙂

    so your modified code will be:

    public static void main(String []a)
    {
       try{
        Session sess = HibernateUtils.getSessionFactory().openSession();
        sess.beginTransaction();
        MessageDetails messageDetails = new MessageDetails();
        messageDetails.setTo(new HashSet<EmailAndName>());
        EmailAndName emailAndName = (EmailAndName) sess.get(EmailAndName.class, 1L);
        if (emailAndName == null ) throw new RuntimeException();
        messageDetails.getTo().add(emailAndName);
        sess.save(messageDetails);
        sess.getTransaction().commit();
     }
    catch(HibernateException e)
     {
       sess.getTransaction.rollback();
       e.printStackTrace();
     }
      finally{
           sess.close();
      }
    

    You must always have a try catch enclosed, so that you can identify the exceptions(if any) and make it work 🙂

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

Sidebar

Related Questions

I have an entity that has a ManyToMany relationship with itself: @Entity public class
Simple JPA/JPQL question. I have an entity with a ManyToMany relationship: @Entity public class
Imagine I have an entity: public class MyObject { public string Name { get;
I have an entity similar to the below: public class Entity { public List<DateItem>
Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
Say, I have following entities: @Entity public class A { @Id @GeneratedValue private Long
Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
I have an abstract entity: public abstract class Entity extends JPanel implements FocusListener And
I have the following entities: @Entity public class Owner{ @Id @Column(name = OWNER_ID) @OneToMany()
I have a Customer class defined like this: @Entity public class Customer { //...

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.