Spingboot React Hypermedia string id



我正在使用Springboot,Hibernate和React构建Web应用程序。我没有使用春季教程用自动化ID创建"正常"对象的麻烦。对于我的用户表,我不太了解需要具有单独索引的自动插入ID,因此我试图将用户名为主键。(这也将使所有参考文献更容易解密(。这是我的用户模型:

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
@Data
@Entity
@Table(name="user")
public class User implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -3924062441897826838L;
    @Id
    @Column(name="username")
    private String userName;
    @Column(name="password")
    private String password;
    @Column(name="role")
    private String role;
    @Column(name="first_name")
    private String firstName;
    @Column(name="last_name")
    private String lastName;
    @Column(name="country")
    private String country;
    @Column(name="enabled")
    private boolean enabled;
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
            },
            mappedBy = "users")
    private Set<Project> projects = new HashSet<>();
    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            mappedBy = "createUser")
    private Set<Project> createdProjects = new HashSet<>();
    private User() {    }
    public User(String userName) {
        this.userName = userName;
    }
    @Override
    public String toString() {
        return "User [userName="+ userName+",firstName="+firstName+"]";
    }

}

我似乎无法通过基本路线访问我的主键(用户名(。(this.props.user.username ... in react(。我了解它已嵌入_links中,但是有什么方法可以访问我的列表页面?

**
 * User Object
 */
class User extends React.Component {
    constructor(props) {
        super(props);
        this.handleDelete = this.handleDelete.bind(this);
    }
    handleDelete() {
        this.props.onDelete(this.props.user);
    }
    render() {
        return (
          <tr>
            <td>**{this.props.user.userName} is BLANK**</td>
            <td>{this.props.user.firstName}</td>
            <td>{this.props.user.lastName}</td>
            <td>{this.props.user.enabled}</td>
            <td>
            <button onClick={this.handleDelete}>Delete</button>
        </td>
          </tr>);
      }
}

是将自动增量ID添加到用户的唯一方法吗?所有的帮助都非常感谢。谢谢!

更新:这是console.log(this.props.user(的输出:

    Object
_links: Object
createdProjects: {href: "http://localhost:8080/api/users/testUserName/createdProjects"}
projects: {href: "http://localhost:8080/api/users/testUserName/projects"}
self: {href: "http://localhost:8080/api/users/testUserName"}
user: {href: "http://localhost:8080/api/users/testUserName"}
Object Prototype
country: "TestCountry"
enabled: true
firstName: "TestName"
lastName: "TestLastName"
password: "testPword"
role: "testrole"

Spring默认情况下不会公开实体类的ID。您可以将其配置为公开某个实体类的ID。下面的代码应进行工作。

public class RestConfiguration extends RepositoryRestConfigurerAdapter {
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(User.class);
    }
}

最新更新