如何在 table Spring + Hibernate + Spring 安全中填充外键

How to populate foreign key in table Spring + Hibernate + Spring Security

我想举个例子,当用户通过 Spring 安全认证,然后他填写地址表时,我想自动更新用户 table 中的外键列 "adres_id" .请告诉我如何以最流行的方式实现它

我怎么会这样

地址Table:

用户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);
    }



}

地址服务

@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 table 中的用户 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;
    }
    ...
}