5

我已经问过这个问题两次了,但是我是 stackoverflow 的新手,而且我似乎不知道在这里格式化我的示例代码的规则。现在我决定给出完整的电话堆栈,我希望我能解释一下情况,因为一切都太奇怪了,我找不到形容词来形容它。首先,我将为您提供与该问题有关的类的来源。我的实际问题在页面末尾。大段代码是为了以防万一,因为我不知道我的问题可能是什么解释。这是一个从我的 flex 应用程序获取调用的服务外观。

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<String> getSocialConnectionsTypes(Long userId) {
        return profileService.getSocialConnectionsTypes(userId);
    }

    @Override
    public List<Email> findDuplicateEmails(Long profileId, List<Email> 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<Pool> 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<Campaign> getCampaigns() {
        return campaignService.getCampaigns();
    }

    @Override
    public void updatePool(Long campaignId, Pool pool) {
        campaignService.updatePool(campaignId, pool);
    }
}

对我的问题很重要的方法是findDuplicateEmails方法。

profileService在以下类中实现:

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<String> getSocialConnectionsTypes(Long userId) {
        return profileManager.getSocialConnectionsTypes(userId);
    }

    @Override
    public List<Email> findDuplicateEmails(Long profileId, List<Email> emails) {
        return profileManager.findDuplicateEmails(profileId, emails);
    }
}

同样重要的方法是findDuplicateEmails

profileManager的实现是以下类:

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<Major> notApprovedMajors = extractNotApprovedMajors(profile);
        List<Degree> notApprovedDegrees = extractNotApprovedDegrees(profile);
        List<School> notApprovedSchools = extractNotApprovedSchools(profile);
        List<Language> notApprovedLanguages = extractNotApprovedLanguages(profile);
        List<Position> notApprovedPositions = extractNotApprovedPositions(profile);
        List<Company> notApprovedCompanies = extractNotApprovedCompanies(profile);
        List<Country> notApprovedCountries = extractNotApprovedCountries(profile);
        List<City> notApprovedCities = extractNotApprovedCities(profile);
        List<Certificate> 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<Email> findDuplicateEmails(Long profileId, List<Email> emails) {

        Profile persistedProfile = profileDao.findById(profileId);

        if (persistedProfile.getContact() == null)
        {
            persistedProfile.setContact(new Contact());
        }

        List<Email> resultEmails = new ArrayList<Email>();

        for (int i = 0; i < emails.size(); i++) {

            if ((!userSecurityService.guaranteeUniquePrincipal(emails.get(i)) &&
                    !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<Email> emails, int index)
    {

        for (int i = index ; i < emails.size(); i ++) {
            if (emails.get(i).getEmailAddress().equals(emailToCheck.getEmailAddress())) {
                return true;
            }
        }

        return false;
    }

    private boolean isPersistedInThePersistentCollection(Email emailToCheck, Collection<Email> emails)
    {
        if (emails == null) {
            return false;
        }
        for (Email persistedEmail : emails) {
            if (persistedEmail.getEmailAddress().equalsIgnoreCase(emailToCheck.getEmailAddress())) {
                return true;
            }
        }

        return false;
    }
}

同样重要的方法是方法findDuplicateEmails

现在,在这个简短的背景之后,这是我的问题:

我正在使用带有 Spring 的 HibernateTemplate 的 Hibernate。我发现在findDuplicateEmails方法中,一些来自 flex 应用程序的全新实体会自动保存。这很奇怪,在调试过程中我发现即使我更改了ProfileManager 中的findDuplicateEmails方法,它看起来像:

 @Override
 public List<Email> findDuplicateEmails(Long profileId, List<Email> emails) {   
     Email email = new Email();
     return null;
 }

实体电子邮件会自动保存。我还发现,如果实体的标识符不是“email”,而是其他东西,比如“newEmail”或“email1”,或者其他东西,那么没有问题,当且仅当我成功时,实体才会持久化执着的。这个问题只存在于这个类中,最后,这个问题只出现在电子邮件中。我的意思是,如果我有Phone phone = new Phone();实体电话,则仅在我希望的时候才会保留。

flex 应用程序首先检查用户输入的电子邮件是否唯一,然后在一些用户交互后调用该方法updateProfile(),如果输入的数据有效。

4

1 回答 1

0

我会下载 Hibernate 源代码并开始调试,您会在 Hibernate(发生)或代码中发现错误,因为这是一种奇怪的行为。这是我曾经得到的建议,也是最快、最具教育意义的方法。

于 2012-02-06T03:22:34.263 回答