java - Spring JDBC 中 Hibernate 模型的等价物是什么

标签 java spring hibernate spring-mvc jdbc

我正在尝试修改此this使用 Spring Security 和 jdbc 创建用户并以权限登录的示例 我现在已经思考并搜索了如何将示例中的一些模型转换为 JDBC。所以我问 JDBC 中的以下 Hibernate 模型的等价物是什么

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name="APP_USER")
public class User implements Serializable{

    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @NotEmpty
    @Column(name="SSO_ID", unique=true, nullable=false)
    private String ssoId;

    @NotEmpty
    @Column(name="PASSWORD", nullable=false)
    private String password;

    @NotEmpty
    @Column(name="FIRST_NAME", nullable=false)
    private String firstName;

    @NotEmpty
    @Column(name="LAST_NAME", nullable=false)
    private String lastName;

    @NotEmpty
    @Column(name="EMAIL", nullable=false)
    private String email;

    @NotEmpty
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "APP_USER_USER_PROFILE", 
             joinColumns = { @JoinColumn(name = "USER_ID") }, 
             inverseJoinColumns = { @JoinColumn(name = "USER_PROFILE_ID") })
    private Set<UserProfile> userProfiles = new HashSet<UserProfile>();
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getSsoId() {
        return ssoId;
    }
    public void setSsoId(String ssoId) {
        this.ssoId = ssoId;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Set<UserProfile> getUserProfiles() {
        return userProfiles;
    }
    public void setUserProfiles(Set<UserProfile> userProfiles) {
        this.userProfiles = userProfiles;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((ssoId == null) ? 0 : ssoId.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof User))
            return false;
        User other = (User) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (ssoId == null) {
            if (other.ssoId != null)
                return false;
        } else if (!ssoId.equals(other.ssoId))
            return false;
        return true;
    }

    /*
     * DO-NOT-INCLUDE passwords in toString function.
     * It is done here just for convenience purpose.
     */
    @Override
    public String toString() {
        return "User [id=" + id + ", ssoId=" + ssoId + ", password=" + password
                + ", firstName=" + firstName + ", lastName=" + lastName
                + ", email=" + email + "]";
    }    
}

@Entity
@Table(name="USER_PROFILE")
public class UserProfile implements Serializable{
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id; 
    @Column(name="TYPE", length=15, unique=true, nullable=false)
    private String type = UserProfileType.USER.getUserProfileType(); 
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof UserProfile))
            return false;
        UserProfile other = (UserProfile) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (type == null) {
            if (other.type != null)
                return false;
        } else if (!type.equals(other.type))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "UserProfile [id=" + id + ", type=" + type + "]";
    }
}

我仍在学习,我想了解 @ManyToMany 和 @JoinTable 注释,我想知道如何在 JDBC 中完成它

最佳答案

以下是Spring支持的数据访问技术,有多种选择。

Spring JDBC 提供了模板,用于减少通过简单的旧方式访问数据库的样板代码 - 编写您自己的 SQL 查询。

Spring-ORM 提供了通过 ORM 技术访问数据库的简化模板,例如 Hibernate、开放 JPA 等。

Spring-DAO:

Spring 中的数据访问对象 (DAO) 支持旨在以一致的方式轻松使用 JDBC、Hibernate 或 JDO 等数据访问技术

关于java - Spring JDBC 中 Hibernate 模型的等价物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40572405/

相关文章:

java - 无法打开 hibernate 连接

java - 如何使用 Hibernate 注释标记外键约束?

Java:保护试用/演示期

java - 请推荐将 apache lucene 与 MVC/spring web 应用程序集成的最佳实践

java - @PathVariable fileName 结果为 406 Not Acceptable

java - 返回 JPA 实体导致内部服务器错误

Java:用 x 百分比的时间做某事

java - Single HQL 中的最小主键和主键

java - not-null 属性引用 null 或 transient 值 : com. project.test.entity.Stamp.createdAt

sql - 有没有SQL + Redis Hybrid ORM/OHM框架?