簡體   English   中英

從另一個實體獲取數據

[英]Getting data from another entity

我必須使用 post 方法添加一個新用戶。 如果location_table中存在用戶提供的locName,則在新的用戶記錄中會考慮location_table中的locName及其對應的狀態,並保存在user_table中。 此代碼在下面的 else 語句中。

@Component 
public class UserManager{
    @Autowired
    private UserRepository uRepo;
    @Autowired
    private LocationRepository lRepo;
    
    @Transactional
    public void save(UserDto u){        
        User user = new User();
                 Location location = new Location();
    
        if(!lRepo.existsByLocName(u.getLocName())){
             user.setBatchNum(u.getBatchNum());
             user.setLocName(u.getLocName());
             user.setState(u.getState());

             location.setLocName(u.getLocName());
             location.setState(u.getState());    
        }
        else{
             user.setBatchNum(u.getBatchNum());
             user.setLocName(u.getLocName());
             user.setState(l.getState ()); // How do I turn this l in “l.getState()” into      
                                           // a variable for Location so I can get the State of
                                           // that Location entity (not in UserDto)?
        }       
        lRepo.save(location);
        uRepo.save(user);
}

嘗試向LocationRepository添加一個方法,它將通過用戶名返回位置狀態,然后將其用作user.setState(...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM