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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:56:26+00:00 2026-05-26T14:56:26+00:00

I got the above mentioned exception when performing following scenario. Students and Address have

  • 0

I got the above mentioned exception when performing following scenario.

Students and Address have Many-To-One relationship where as Student and PhoneNumbers have One-To-Many relationship. On calling persist method on EntityManager object for saving ‘Students’ object, I got exception as follows:

org.hibernate.TransientObjectException: object references an unsaved
transient instance – save the transient instance before flushing:
model.Students1.addressId -> model.Address

What steps can be taken to resolve it?

Details are as follows:

DAO class :

    public class DAO {

    public static void main(String[] arr){

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("OneToManyPU");

    EntityManager em = emf.createEntityManager();

    EntityTransaction tr= em.getTransaction();

    try{
        tr.begin();

        PhoneNumbers p1 = new PhoneNumbers();
        PhoneNumbers p2 = new PhoneNumbers();

        p1.setPhoneType("mobile");
        p1.setPhoneNo("9881592106");

        p2.setPhoneType("landline");
        p2.setPhoneNo("24214988");

        Set<PhoneNumbers> phones = new HashSet<PhoneNumbers>();
        phones.add(p1);
        phones.add(p2);

        em.persist(p1);
        em.persist(p2);

        Address a1 = new Address();
        a1.setCity("Pune");
        a1.setZip("400987");
        Students1 s1 = new Students1();
        s1.setName("Alka");
        s1.setAddressId(a1);
        s1.setPhoneNo(phones);

        em.persist(s1);
         tr.commit();
   }
    catch(Exception e){
       e.printStackTrace();
    }
    finally{
         emf.close();
    }
}
    }  

Students1 class:

    @Entity    
@Table(name = "STUDENTS")    
public class Students1 implements Serializable {    
    private static final long serialVersionUID = 1L;     
    @Id    
    @GeneratedValue(strategy = GenerationType.AUTO)     
    @Column(name = "ID")    
    private Long id;    

    @Column(name = "NAME")    
    private String name;    

    @JoinColumn(name = "ADDRESS_ID", referencedColumnName = "ID")    
    @ManyToOne    
    private Address addressId;  

    @OneToMany(cascade ={CascadeType.MERGE,CascadeType.PERSIST})
   @JoinTable(name="STUDENT_PHONE",joinColumns={@JoinColumn(name="STUDENTS.ID")},inverseJoinColumns={@JoinColumn(name="PHONENUMBERS.ID")})
    private Set<PhoneNumbers> phoneNo = new HashSet<PhoneNumbers>();  

       public void setPhoneNo(Set<PhoneNumbers> phoneNo) {  
        this.phoneNo = phoneNo;
    }

    public Set<PhoneNumbers> getPhoneNo() {
        return phoneNo;
    }

    public Students1() {
    }

    public Students1(Long id) {
        this.id = id;
    }
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddressId() {
        return addressId;
    }

    public void setAddressId(Address addressId) {
        this.addressId = addressId;
    }

}

Address Class

@Entity  
@Table(name = "ADDRESS")  
public class Address implements Serializable {  
    private static final long serialVersionUID = 1L;  
    @Id  
    @GeneratedValue(strategy = GenerationType.AUTO)  
    @Column(name = "ID")  
    private Long id;  
    @Column(name = "CITY")  
    private String city;  
    @Column(name = "ZIP")  
    private String zip;  
    @OneToMany(mappedBy = "addressId")  
    private Collection<Students1> students1Collection;  

    public Address() {
    }

    public Address(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public Collection<Students1> getStudents1Collection() {
        return students1Collection;
    }

    public void setStudents1Collection(Collection<Students1> students1Collection) {
        this.students1Collection = students1Collection;
    }  
}

PhoneNumbers class

    @Entity 
    public class PhoneNumbers implements Serializable {    
    private static final long serialVersionUID = 1L;    
    @Id     
    @GeneratedValue(strategy = GenerationType.AUTO)    
    private Long id;    

    @Column(name="PhoneNo")  
    private String phoneNo;

    @Column(name="PhoneType")  
    private String phoneType;

    public String getPhoneNo() {  
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {  
        this.phoneNo = phoneNo;
    }

    public String getPhoneType() {
        return phoneType;
    }

    public void setPhoneType(String phoneType) {
        this.phoneType = phoneType;
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    } 
 }
  • 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-26T14:56:27+00:00Added an answer on May 26, 2026 at 2:56 pm

    You should define cascades.
    A cascade means that if object ‘A’ has an Object ‘B’ (or a collection of those) then the referenced ‘B’ objects are saved/updated as your cascade definition indicates.

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

Sidebar

Related Questions

I got an error as I mentioned above. My application has EJB WebServices, developing
I've got following classes: public class Container { private String name; private Data data;
I've got the following line of code in my program: $('input').filter(':radio').change(function() { The problem
Hello Friends i have the following query which defeat the very purpose. A day
I've got a situation where I need to have my LINQ to Entities query
I've got the following problem. My table ( geo_table ) structure is as follows:
I tried and got the following output: [object Object] I am familiar with adding
I've got three activities in my application and I have problems with calling the
I have Visual Studio 2010 installed and have a project I got from someone
I have a single thread producer which creates some task objects which are then

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.