gpt4 book ai didi

java - 如何在 Spring + Hibernate + Spring Security 表中填充外键

转载 作者:行者123 更新时间:2023-12-03 06:05:01 25 4
gpt4 key购买 nike

我想举个例子,当用户通过 Spring Security 进行身份验证,然后他填写地址表单时,我想自动更新用户表中的外键列“adres_id”。请给我一个提示,如何以最流行的方式实现这一点

我怎么会有这样的事情

地址表: Address Table

用户表: User Table

地址

@Entity
@Table(name="adres")
public class Adres {


@Id
@GeneratedValue(strategy = GenerationType.AUTO )
int id;

@Column(name="country", nullable=false)
private String country;
private String street;
private String postcode;
private String telephone;
private String pesel;


@OneToOne(mappedBy ="adres")
private User user;




public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}

public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getPesel() {
return pesel;
}
public void setPesel(String pesel) {
this.pesel = pesel;
}
public String getStreet() {
return postcode;
}
public void setStreet(String street) {
this.street = street;
}

public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}


}

用户

@Entity
@Table(name="users")
public class User {


@Id
@GeneratedValue(strategy = GenerationType.AUTO )
int id;

@Column(name="username", nullable=false)
private String username;
private String password;
private String email;
private Boolean enabled;



@OneToOne(cascade = CascadeType.ALL)
private Adres adres;


public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

}

AdresDAO

@Repository
@Transactional
public class AdresDAOImpl implements AdresDAO{

@Autowired
SessionFactory sessionFactory;

public void addAdres(Adres adres) {
sessionFactory.getCurrentSession().save(adres);
}

public List<Adres> listAdres() {

return sessionFactory.getCurrentSession().createQuery("from Adres order by id").list();
}

public void removeAdres(int id) {
Adres adres = (Adres) sessionFactory.getCurrentSession().load(
Adres.class, id);
if (null != adres) {
sessionFactory.getCurrentSession().delete(adres);
}

}
public Adres getAdres(int id) {

return (Adres)sessionFactory.getCurrentSession().get(Adres.class, id);
}

public void editAdres(Adres adres) {

sessionFactory.getCurrentSession().update(adres);
}



}

AdresService

@Service
public class AdresServiceImpl implements AdresService{

@Autowired
AdresDAO adresDAO;



@Transactional
public void addAdres(Adres adres) {
adresDAO.addAdres(adres);

}

@Transactional
public void editAdres(Adres adres) {
adresDAO.editAdres(adres);
}

@Transactional
public List<Adres> listAdres() {

return adresDAO.listAdres();
}

@Transactional
public void removeAdres(int id) {
adresDAO.removeAdres(id);
}

@Transactional
public Adres getAdres(int id) {
return adresDAO.getAdres(id);
}


}

最佳答案

User UserAddress 之间的单向关系,如果 Address 对象不应该知道其所有者(一般情况下不会)。如果 User 有多个 Address (一对多关系),我更喜欢在 Address 表中使用用户 ID。

但是对于你的问题,你可以这样设计,

public class User{
...
@OneToOne(CascadeType.REMOVE)//this is for to remove address when user is removed
@JoinColumn(name="HOME_ADDRESS_ID")
private Address address;
...
}

public class Address {

@Id
@GeneratedValue(strategy = GenerationType.AUTO )
int id;

@Column(name="country", nullable=false)
private String country;
private String street;
private String postcode;
private String telephone;
private String pesel;

//no user object here

public int getId() {
return id;
}
...
}

关于java - 如何在 Spring + Hibernate + Spring Security 表中填充外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37530845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com