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.
You havent read through hibernate-doc for stateless session
It clearly states that:
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:
You must always have a try catch enclosed, so that you can identify the exceptions(if any) and make it work 🙂