如何向用户(jhi_用户)实体Jhipster添加字段?
我想向如何向用户(jhi_用户)实体Jhipster添加字段?,jhipster,Jhipster,我想向用户实体添加新字段说明。我不能使用yojhipster:entity User,因为它是被禁止的。那么,我该怎么办 将列添加到实体User.java: @Column(name = "description") private String description; 修改方法toString() 在UserDTO.java中添加: private String description; 生成getter并修改方法toString() 添加液基差异或添加列 现在是时候修改一些webap
用户
实体添加新字段说明
。我不能使用yojhipster:entity User
,因为它是被禁止的。那么,我该怎么办
User.java
:
@Column(name = "description")
private String description;
修改方法toString()
UserDTO.java
中添加:
private String description;
toString()
webapp
文件夹了
user management.state.js
find stateuser management.new
和toentity:function()
new return,description:null
user management.html
user management detail.html
用户管理对话框.html
例如:
Description
如果我们需要存储更多关于用户的信息,而不是JHipster默认提供的信息,则需要进行一些调整
为了说明这一点,我们假设要存储用户的电话号码
与JHI_用户以一对一关系创建新实体
添加默认JHipster用户未处理的信息的最佳方法是在一个具有一对一关系的新实体中使用组合
创建这个实体后,我们称之为UserExtra,处理其id的最佳方法是将其映射到JHI_用户的id。这样,我们的UserExtra将拥有与用户相同的id,从而加速不同的请求。要实现这一点,您需要使用@MapsId注释:
public class UserExtra implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@Column(name = "phone")
private String phone;
@OneToOne
@MapsId
private User user;
...
}
请注意,需要删除id上的@GeneratedValue注释
更新注册HTML页面以考虑此更改
现在已经存在一个实体来存储电话号码,我们需要在注册表中添加一个输入来请求用户的电话号码
没有比这更简单的了,只需更新webapp/app/account/register/register.html,添加一个绑定到已用于存储基本信息的变量的输入字段(vm.registerAccount):
<input class="form-control" id="phone" ng-model="vm.registerAccount.phone" placeholder="global.form.phone.placeholder" />
从AccountResource更新registerAccount()函数
registerAccount()函数现在接收一个ManagedUserVM对象,该对象还包含用户的电话号码。剩下要做的唯一一件事就是将此电话号码保存到与JHipster用户关联的新UserExtra中
为此,我们将从UserService向createUser()函数添加phone参数。但首先,在registerAccount()中调用此函数的位置添加此参数:
public ResponseEntity registerAccount(@Valid@RequestBody ManagedUserVM ManagedUserVM){
HttpHeaders textPlainHeaders=新的HttpHeaders();
textPlainHeaders.setContentType(MediaType.TEXT\u PLAIN);
返回userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase())
.map(用户->新响应属性(“登录已在使用”,textplanheaders,HttpStatus.BAD_请求))
.orElseGet(()->userRepository.findOneByEmail(managedUserVM.getEmail())
.map(用户->新响应属性(“已在使用的电子邮件地址”、textPlainHeaders、HttpStatus.BAD_请求))
.orElseGet(()->{
用户=用户服务
.createUser(managedUserVM.getLogin(),managedUserVM.getPassword(),
managedUserVM.getFirstName(),managedUserVM.getLastName(),
managedUserVM.getEmail().toLowerCase(),managedUserVM.getLangKey(),
managedUserVM.getPhone());
mailService.sendActivationEmail(用户);
返回新的ResponseEntity(HttpStatus.CREATED);
})
);
}
从UserService更新createUser()函数
最后,我们将保存JHI_用户的服务层函数更新为现在保存UserExtra。我建议您使用附加参数创建一个新函数,而不是更新现有函数。这样,就不需要更新测试类
不要忘记注入UserExtra存储库:
@Inject
private UserExtraRepository userExtraRepository;
@Inject
private UserExtraSearchRepository userExtraSearchRepository;
...
public User createUser(String login, String password, String firstName, String lastName, String email,
String langKey, String phone) {
User newUser = new User();
Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER);
Set<Authority> authorities = new HashSet<>();
String encryptedPassword = passwordEncoder.encode(password);
newUser.setLogin(login);
// new user gets initially a generated password
newUser.setPassword(encryptedPassword);
newUser.setFirstName(firstName);
newUser.setLastName(lastName);
newUser.setEmail(email);
newUser.setLangKey(langKey);
// new user is not active
newUser.setActivated(false);
// new user gets registration key
newUser.setActivationKey(RandomUtil.generateActivationKey());
authorities.add(authority);
newUser.setAuthorities(authorities);
userRepository.save(newUser);
userSearchRepository.save(newUser);
log.debug("Created Information for User: {}", newUser);
// Create and save the UserExtra entity
UserExtra newUserExtra = new UserExtra();
newUserExtra.setUser(newUser);
newUserExtra.setPhone(phone);
userExtraRepository.save(newUserExtra);
userExtraSearchRepository.save(newUserExtra);
log.debug("Created Information for UserExtra: {}", newUserExtra);
return newUser;
}
@Inject
私有用户外部存储库用户外部存储库;
@注入
私有用户ExtraSearchRepository用户ExtraSearchRepository;
...
公共用户createUser(字符串登录、字符串密码、字符串名字、字符串姓氏、字符串电子邮件、,
(串键、串电话){
User newUser=新用户();
Authority=authorityRepository.findOne(AuthoritiesConstants.USER);
设置你可以生成一个新的实体UserExtra,并与用户建立一对一的关系。是的,但在我看来,这是一个更好的选择。你不必创建很多不必要的代码。你的想法也会正常工作。yo jhipster:upgrade
是否保留你对用户相关类或屏幕的自定义?我猜,jhipster可能会请求权限覆盖冲突文件。如果我添加其他文件,jhipster总是会询问。@Ramespaul,我猜。但在过去三年中,jhipster上有很多内容可能会更改。是的,我同意。做了很多更改。
public ResponseEntity<?> registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
HttpHeaders textPlainHeaders = new HttpHeaders();
textPlainHeaders.setContentType(MediaType.TEXT_PLAIN);
return userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase())
.map(user -> new ResponseEntity<>("login already in use", textPlainHeaders, HttpStatus.BAD_REQUEST))
.orElseGet(() -> userRepository.findOneByEmail(managedUserVM.getEmail())
.map(user -> new ResponseEntity<>("e-mail address already in use", textPlainHeaders, HttpStatus.BAD_REQUEST))
.orElseGet(() -> {
User user = userService
.createUser(managedUserVM.getLogin(), managedUserVM.getPassword(),
managedUserVM.getFirstName(), managedUserVM.getLastName(),
managedUserVM.getEmail().toLowerCase(), managedUserVM.getLangKey(),
managedUserVM.getPhone());
mailService.sendActivationEmail(user);
return new ResponseEntity<>(HttpStatus.CREATED);
})
);
}
@Inject
private UserExtraRepository userExtraRepository;
@Inject
private UserExtraSearchRepository userExtraSearchRepository;
...
public User createUser(String login, String password, String firstName, String lastName, String email,
String langKey, String phone) {
User newUser = new User();
Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER);
Set<Authority> authorities = new HashSet<>();
String encryptedPassword = passwordEncoder.encode(password);
newUser.setLogin(login);
// new user gets initially a generated password
newUser.setPassword(encryptedPassword);
newUser.setFirstName(firstName);
newUser.setLastName(lastName);
newUser.setEmail(email);
newUser.setLangKey(langKey);
// new user is not active
newUser.setActivated(false);
// new user gets registration key
newUser.setActivationKey(RandomUtil.generateActivationKey());
authorities.add(authority);
newUser.setAuthorities(authorities);
userRepository.save(newUser);
userSearchRepository.save(newUser);
log.debug("Created Information for User: {}", newUser);
// Create and save the UserExtra entity
UserExtra newUserExtra = new UserExtra();
newUserExtra.setUser(newUser);
newUserExtra.setPhone(phone);
userExtraRepository.save(newUserExtra);
userExtraSearchRepository.save(newUserExtra);
log.debug("Created Information for UserExtra: {}", newUserExtra);
return newUser;
}