如何用角度保存实体中的关系

时间:2017-12-01 16:08:07

标签: angularjs foreign-keys jhipster

我正在使用AngularJS和JHipster来开发一个应用程序。我有两个与他们有关系的实体:DemandelettreEtudiant。我需要手动将Etudiant权限密钥保存到Demandelettre权限,但是当我尝试创建它时会出现一些错误。

我需要用输入文本替换它,以便将输入值保存到其他权限,例如id for relationship。

<div class="form-group">
        <label for="field_etudiant">Etudiant</label>
        <select class="form-control" id="field_etudiant" name="etudiant" ng-model="vm.demandelettre.etudiant" ng-options="etudiant as etudiant.matricule for etudiant in vm.etudiants track by etudiant.id">
            <option value=""></option>
        </select>
    </div>

这是save控制器中的默认Demandelettre方法:

function save () {
        vm.isSaving = true;
        if (vm.demandelettre.id !== null) {
            vm.demandelettre.etattraitement = 1;
            Demandelettre.update(vm.demandelettre, onSaveSuccess, onSaveError);
        } else {
            vm.demandelettre.datedemande = Date.now() ,'yyyy-MM-dd' ;
            vm.demandelettre.etattraitement = 0;
            Demandelettre.save(vm.demandelettre, onSaveSuccess, onSaveError);
        }
    }

有没有人有解决办法手动将此外键保存在该权利中? 对不起我的英语(如果穷人)。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案。您可以观看此视频了解更多详情。链接:https://youtu.be/pfK6njKkr0w

我创建了课程DemandelettreEtudiantDao,我把它放在里面:

package org.uasz.cri.web.rest;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.uasz.cri.security.SecurityUtils;

@Component

public class DemandelletreEtudiantDao {
    private final JdbcTemplate jdbcTemplate;
    public DemandelletreEtudiantDao(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }
    public long getEtudiantIdByCurrentLogin(){
        String sql="select id from etudiant where email='"+ SecurityUtils.getCurrentUserLogin()+"'";
        System.out.println("Id Etudiant"+sql);
        return this.jdbcTemplate.queryForObject(sql, long.class);
    }

}

在DemandelettreResource.java中,我调用了DemandelletreEtudiantDao类方法:

// Call DemandelletreEtudiantDao class method
    private UserRepository userRepository;
    private DemandelletreEtudiantDao demandelletreEtudiantDao;
    private EtudiantRepository etudiantRepository;

// Class constructor
    @Autowired
    public DemandelettreResource(
            DemandelettreRepository demandelettreRepository,
            UserRepository userRepository,
            EtudiantRepository etudiantRepository,
            DemandelletreEtudiantDao demandelletreEtudiantDao) {
        super();
        this.demandelettreRepository = demandelettreRepository;
        this.userRepository = userRepository;
        this.etudiantRepository = etudiantRepository;
        this.demandelletreEtudiantDao = demandelletreEtudiantDao;
    }

// over post method
        demandelettre.setEtudiant(etudiantRepository.findOne(demandelletreEtudiantDao.getEtudiantIdByCurrentLogin()));