【问题标题】:Mapping/Navigation AngularJs Object to Hibernate entity将 AngularJs 对象映射/导航到 Hibernate 实体
【发布时间】:2016-03-23 07:43:39
【问题描述】:

我有一个 angularjs/Spring/Hibernate 应用程序,我正在使用 REST 服务在 angularjs 和服务器端之间进行交互。 我想将一个用户保留到 DB,所以这就是我的 angularjs 方面的外观。

createUser: function(user){
            var userToSend = {
                        "firstName" : user.firstName,
                        "lastName" : user.lastName,
                        "homeAddress.location" : user.homeAddress.location  ,
                        "email" : user.email,
                        "ssoId": user.ssoId
                };
                    var a= $http.post('http://localhost:8080/MyDirectory/createUser/', userToSend)
                            .then(
                                    function(response){
                                      $rootScope.refresh();
                                        return response.data;
                                    }, 
                                    function(errResponse){
                                         window.alert(errResponse);
                                        console.error('Error while creating user');
                                        return $q.reject(errResponse);
                                    }
                            );

                            return null;
            },

我的服务器端是这样的:

@Transactional(propagation = Propagation.REQUIRED)
    public void saveUser(User user) {
        Long nextLong = RandomUtils.nextLong(0, 10000L);
        // user.setId(nextLong.intValue());
        User merge = em.merge(user);
        System.out.println(" merged "); 
        em.persist(merge); 

        System.out.println("");
    }

这是我的用户和家庭地址实体:

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

    private static final long serialVersionUID = 1L;

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

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

    @NotEmpty

    private String firstName;

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

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

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<UserInscription> userInscription = new HashSet<UserInscription>();

    @OneToOne
//  @JsonManagedReference
    private HomeAddress homeAddress;

    public Set<UserInscription> getUserInscription() {
        return userInscription;
    }

    public void setUserInscription(Set<UserInscription> userInscription) {
        this.userInscription = userInscription;
    }

    // @JsonIgnore
    public HomeAddress getHomeAddress() {
        return homeAddress;
    }

    public void setHomeAddress(HomeAddress homeAddress) {
        this.homeAddress = homeAddress;
    }

    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 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;
    }

    @JsonIgnore
    public Set<UserInscription> getUserDocuments() {
        return userInscription;
    }

    public void setUserDocuments(Set<UserInscription> UserInscriptions) {
        this.userInscription = UserInscriptions;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", ssoId=" + ssoId + ", firstName=" + firstName + ", lastName=" + lastName
                + ", email=" + email + "]";
    }

}







@Entity
public class HomeAddress implements Serializable {

    private static final long serialVersionUID = 1L;

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

    private String email;
    @OneToOne
    private Country country;
    private String location;
    @JsonBackReference
    @OneToOne
    private User relatedUser;

    public User getRelatedUser() {
        return relatedUser;
    }

    public void setRelatedUser(User relatedUser) {
        this.relatedUser = relatedUser;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

现在,如果我没有 homeaddress 值。它可以正常工作,但 homeaddress 是 OneToOne 。我遇到了这个异常:

23-Mar-2016 11:35:00.081 WARNING [http-nio-8080-exec-33] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized field "homeAddress.location" (class com.directory.model.User), not marked as ignorable (7 known properties: "lastName", "homeAddress", "ssoId", "id", "firstName", "email", "userInscription"])
 at [Source: java.io.PushbackInputStream@93d96d; line: 1, column: 61] (through reference chain: com.directory.model.User["homeAddress.location"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "homeAddress.location" (class com.directory.model.User), not marked as ignorable (7 known properties: "lastName", "homeAddress", "ssoId", "id", "firstName", "email", "userInscription"])
 at [Source: java.io.PushbackInputStream@93d96d; line: 1, column: 61] (through reference chain: com.directory.model.User["homeAddress.location"])

【问题讨论】:

    标签: java angularjs spring hibernate spring-mvc


    【解决方案1】:

    这样发布:

     var userToSend = {
                            "firstName" : user.firstName,
                            "lastName" : user.lastName,
                            "homeAddress":{location : user.homeAddress.location},
                            "email" : user.email,
                            "ssoId": user.ssoId
                    };
    

    即使您可以在 JS/Java 中使用“.”来使用它这不是 JSON 的构建方式。 Json 是对象,子对象必须像我发布的那样定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      相关资源
      最近更新 更多