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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:26:52+00:00 2026-05-24T02:26:52+00:00

i have a Problem while testing my DAOObject. I have two Entitys which are

  • 0

i have a Problem while testing my DAOObject. I have two Entitys which are linked by a @oneToOne relation.
The Classes are:

@Entity
@Table
public class Device extends HyEntity {

    @OneToOne(mappedBy = "device")
    private EndUser endUser;
    //getters+setters
}

and

@Entity
@Table
public class EndUser extends HyEntity {

    private String firstname;
    private String lastname;
    @OneToOne
    private Device device;
    //getters+setters
}

In the DAO i do the Following:

    Device d1 = new Device();

    EndUser e1 = new EndUser();
    e1.setFirstname("Hans");
    e1.setLastname("Muster");
    e1.setDevice(d1);

    repo.saveEntity(d1);
    repo.saveEntity(e1);
    sf.getCurrentSession().flush();
    repo.updateEntity(d1);
    sf.getCurrentSession().flush();

    Assert.notNull(repo.getEndUserById(e1.getId()));
    Assert.notNull(repo.getEndUserById(e1.getId()).getDevice());
    Assert.notNull(repo.getDeviceById(d1.getId()));

    sf.getCurrentSession().flush();
    d1 = repo.getDeviceById(d1.getId());
    Hibernate.initialize(d1); 
    Hibernate.initialize(d1.getEndUser());
    Assert.notNull(repo.getDeviceById(d1.getId()).getEndUser()); //FAILS endUser of Device is always NULL

As you can see, neither session.flush() nor Hibernate.Initalize works. The endUser is not assigend to the device.
If I remove the @Transactional, … everything works as it should.
Is there a way to get this to work?
Otherwise i’ll always have to delete the created entities in the Database manually after the test, which is pretty anoying.

Hoping for an Answer.

Thanks in Advance.

  • 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-24T02:26:52+00:00Added an answer on May 24, 2026 at 2:26 am

    You need to add a clear() after the last flush. If you miss that, than hibernate will return exactly the SAME instance from its internal cache, where the releationship is not updated.

    Anyway in my personal opinion it is bad practice not to set the relation ship on both sides by hand. — Because if you do not set it on both sides, you always must take care that the entity is reloaded from the database. And if you do not reload the entity from the database than … (see your test).

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

Sidebar

Related Questions

I found some problem while testing my NLP system. I have a java regex
I have problem while using jquery maskedinput with asp.net textbox. I have a check
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
I have a problem while pasting my contents (or text) generated by Java code
I have a problem while executing a SSIS script component. To be honest I
I have a problem while scrolling images on tableview. I am getting a Signal
I have come across an annoying problem while writing some PHP4 code. I renamed
I have a flash file menu i am having problem while link it to
Hey dudes.i am having this problem while symlinking. I have successfully deployed a ruby
I have this problem I've been trying to tackle for a while. I have

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.