Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate is saving a completely new entity automatically(full stack of calls)
    text
    copied!<p>I have already asked this question two times, but I'm new to stackoverflow and it seems that I don't know the rules for formatting my example code in here. Now I've decided to give the full stack of the calls and I hope I can explain the situation because everything is so strange and I can't find the words to describe it. First I will give you the source of the classes that have something to do with the problem. My actual question is in the end of the page. The large piece of code is just in case, because I don't know what could be the explanation of my problem. Here is a service facade that gets calls from my flex application.</p> <pre><code>public class ServiceFacade implements IAuthenticationService, IProfileService, ICampaignService { @Autowired private IAuthenticationService authenticationService; @Autowired private IProfileService profileService; @Autowired private ICampaignService campaignService; public void login(User user) throws AuthenticationException{ authenticationService.login(user); } @Override public void logout() throws AuthenticationException { authenticationService.logout(); } @Override public void sendForgottenPassword(String email) { authenticationService.sendForgottenPassword(email); } @Override public Profile getProfile(Long userId) { return profileService.getProfile(userId); } @Override public Profile updateProfile(Profile profile) { return profileService.updateProfile(profile); } @Override public Collection&lt;String&gt; getSocialConnectionsTypes(Long userId) { return profileService.getSocialConnectionsTypes(userId); } @Override public List&lt;Email&gt; findDuplicateEmails(Long profileId, List&lt;Email&gt; emails) { return profileService.findDuplicateEmails(profileId, emails); } @Override public Campaign getCampaign(Long campaignId) { return campaignService.getCampaign(campaignId); } @Override public Campaign updateCampaign(Campaign campaign) { return campaignService.updateCampaign(campaign); } @Override public void removeCampaign(Long campaignId) { campaignService.removeCampaign(campaignId); } @Override public void setPools(Long campaignId, Collection&lt;Pool&gt; pools) { campaignService.setPools(campaignId, pools); } @Override public void addPool(Long campaignId, Pool pool) { campaignService.addPool(campaignId, pool); } @Override public void removePool(Long campaignId, Pool pool) { campaignService.removePool(campaignId, pool); } @Override public List&lt;Campaign&gt; getCampaigns() { return campaignService.getCampaigns(); } @Override public void updatePool(Long campaignId, Pool pool) { campaignService.updatePool(campaignId, pool); } } </code></pre> <p>The method which is important for my question is the <strong>findDuplicateEmails</strong> method.</p> <p>The <em>profileService</em> is implemented in the following class:</p> <pre><code>public class ProfileService implements IProfileService { @Autowired private IProfileManager profileManager; @Override public Profile getProfile(Long userId) { return profileManager.getProfile(userId); } @Override public Profile updateProfile(Profile profile){ profileManager.updateProfile(profile); return profile; } @Override public Collection&lt;String&gt; getSocialConnectionsTypes(Long userId) { return profileManager.getSocialConnectionsTypes(userId); } @Override public List&lt;Email&gt; findDuplicateEmails(Long profileId, List&lt;Email&gt; emails) { return profileManager.findDuplicateEmails(profileId, emails); } } </code></pre> <p>Again the important method is <strong>findDuplicateEmails</strong></p> <p>The implementation of the <em>profileManager</em> is the following class:</p> <pre><code>public class ProfileManager implements IProfileManager { @Autowired private IProfileDao profileDao; @Autowired private ISectionManager autoCompleteManager; @Autowired private IUserSecurityService userSecurityService; @Transactional public Profile getProfile(Long userId) { return profileDao.getProfileByUser(userId); } @Transactional public void updateProfile(final Profile profile) { List&lt;Major&gt; notApprovedMajors = extractNotApprovedMajors(profile); List&lt;Degree&gt; notApprovedDegrees = extractNotApprovedDegrees(profile); List&lt;School&gt; notApprovedSchools = extractNotApprovedSchools(profile); List&lt;Language&gt; notApprovedLanguages = extractNotApprovedLanguages(profile); List&lt;Position&gt; notApprovedPositions = extractNotApprovedPositions(profile); List&lt;Company&gt; notApprovedCompanies = extractNotApprovedCompanies(profile); List&lt;Country&gt; notApprovedCountries = extractNotApprovedCountries(profile); List&lt;City&gt; notApprovedCities = extractNotApprovedCities(profile); List&lt;Certificate&gt; notApprovedCertificates = extractNotApprovedCertificates(profile); autoCompleteManager.updateAll(notApprovedMajors); autoCompleteManager.updateAll(notApprovedDegrees); autoCompleteManager.updateAll(notApprovedSchools); autoCompleteManager.updateAll(notApprovedLanguages); autoCompleteManager.updateAll(notApprovedPositions); autoCompleteManager.updateAll(notApprovedCompanies); autoCompleteManager.updateAll(notApprovedCountries); autoCompleteManager.updateAll(notApprovedCities); autoCompleteManager.updateAll(notApprovedCertificates); profileDao.updateProfile(profile); } @Override public List&lt;Email&gt; findDuplicateEmails(Long profileId, List&lt;Email&gt; emails) { Profile persistedProfile = profileDao.findById(profileId); if (persistedProfile.getContact() == null) { persistedProfile.setContact(new Contact()); } List&lt;Email&gt; resultEmails = new ArrayList&lt;Email&gt;(); for (int i = 0; i &lt; emails.size(); i++) { if ((!userSecurityService.guaranteeUniquePrincipal(emails.get(i)) &amp;&amp; !isPersistedInThePersistentCollection(emails.get(i), persistedProfile.getContact().getEmails())) || isDuplicateInTheCurrentCollection(emails.get(i), emails, i + 1)) { resultEmails.add(emails.get(i)); } } return resultEmails; } private boolean isDuplicateInTheCurrentCollection(Email emailToCheck, List&lt;Email&gt; emails, int index) { for (int i = index ; i &lt; emails.size(); i ++) { if (emails.get(i).getEmailAddress().equals(emailToCheck.getEmailAddress())) { return true; } } return false; } private boolean isPersistedInThePersistentCollection(Email emailToCheck, Collection&lt;Email&gt; emails) { if (emails == null) { return false; } for (Email persistedEmail : emails) { if (persistedEmail.getEmailAddress().equalsIgnoreCase(emailToCheck.getEmailAddress())) { return true; } } return false; } } </code></pre> <p>Again the important method is the method <strong>findDuplicateEmails</strong></p> <p>Now, after this short background, here is my problem: </p> <p>I am using Hibernate with spring's HibernateTemplate. I found out that in the method <strong>findDuplicateEmails</strong>, some completely new entities which come form the flex application gets saved automatically. This was very strange and during the debbugging I found out that even if I change the method <strong>findDuplicateEmails</strong> in the <em>ProfileManager</em> so it looks like:</p> <pre><code> @Override public List&lt;Email&gt; findDuplicateEmails(Long profileId, List&lt;Email&gt; emails) { Email email = new Email(); return null; } </code></pre> <p>the entity email gets saved automatically. I also found out that if the identifier of the entity is not "email", but something else, like "newEmail", or "email1", or something, there is no problem and the entity gets persisted if and only if I make it persistent. This problem exists only in this class and finally, this problem shows up only for the Email. I mean that if I have <code>Phone phone = new Phone();</code> the entity phone gets persisted only when I wish.</p> <p>The flex application first checks that the entered from the user emails are unique, and then after some user interaction calls the method <code>updateProfile()</code> if the entered data is valid. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload