2016-06-25 4 views
1

springブートで単純な保存jpa/hibernateは、データベースにヌルの行を保存します。Spring Jpa - hibernateはデータベースにヌルの行を保存します

package rest.api.entity; 

import com.fasterxml.jackson.annotation.JsonIgnore; 

import javax.persistence.*; 
import java.sql.Blob; 
import java.sql.Date; 


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

    @Id 
    @GeneratedValue(strategy= GenerationType.AUTO) 
    @Column(name="id") 
    private @JsonIgnore Long id; 

    @Column(name="created") 
    private Date created; 

    @Column(name="latitude") 
    private @JsonIgnore 
    Float latitude; 

    @Column(name="longitude") 
    private @JsonIgnore Float longitude; 

    @Column(name="password") 
    private @JsonIgnore String password; 

    @Column(name="user_name") 
    private @JsonIgnore String username; 

    @Column(name="pic_thumbnail") 
    private @JsonIgnore Blob picThumbnail; 

    @Column(name="first_name") 
    private @JsonIgnore String firstName; 

    @Column(name="middle_name") 
    private @JsonIgnore String middleName; 
    @Column(name="last_name") 
    private @JsonIgnore String lastName; 

    @Column(name="location_of_residence") 
    private @JsonIgnore String locationOfResidence; 

    @Column(name="telephone") 
    private @JsonIgnore String telephone; 

    @Column(name="email") 
    private @JsonIgnore String email; 

    @Column(name="description") 
    private @JsonIgnore String description; 

    @Column(name="date_of_birth") 
    private @JsonIgnore 
    java.sql.Date dob = null; 

    protected User() {} 

    public User(Long id, Date created, Float latitude, Float longitude, String password, String username, Blob picThumbnail, String firstName, String middleName, String lastName, String locationOfResidence, String telephone, String email, String description, Date dob) { 
     this.id = id; 
     this.created = created; 
     this.latitude = latitude; 
     this.longitude = longitude; 
     this.password = password; 
     this.username = username; 
     this.picThumbnail = picThumbnail; 
     this.firstName = firstName; 
     this.middleName = middleName; 
     this.lastName = lastName; 
     this.locationOfResidence = locationOfResidence; 
     this.telephone = telephone; 
     this.email = email; 
     this.description = description; 
     this.dob = dob; 
    } 


    // constructor for Logindetails 
    public User(Long id, String username, String password, String email) { 
     this.id = id; 
     this.created = null; 
     this.latitude = null; 
     this.longitude = null; 
     this.password = password; 
     this.username = username; 
     this.picThumbnail = null; 
     this.firstName = null; 
     this.middleName = null; 
     this.lastName = null; 
     this.locationOfResidence = null; 
     this.telephone = null; 
     this.email = email; 
     this.description = null; 
     this.dob = null; 
    } 

    public User(String username, String email, String password) { 
    } 

    public Long getId() { 
     return id; 
    } 

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

    public Date getCreated() { 
     return created; 
    } 

    public void setCreated(Date created) { 
     this.created = created; 
    } 

    public Float getLatitude() { 
     return latitude; 
    } 

    public void setLatitude(Float latitude) { 
     this.latitude = latitude; 
    } 

    public Float getLongitude() { 
     return longitude; 
    } 

    public void setLongitude(Float longitude) { 
     this.longitude = longitude; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public Blob getPicThumbnail() { 
     return picThumbnail; 
    } 

    public void setPicThumbnail(Blob picThumbnail) { 
     this.picThumbnail = picThumbnail; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getMiddleName() { 
     return middleName; 
    } 

    public void setMiddleName(String middleName) { 
     this.middleName = middleName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getLocationOfResidence() { 
     return locationOfResidence; 
    } 

    public void setLocationOfResidence(String locationOfResidence) { 
     this.locationOfResidence = locationOfResidence; 
    } 

    public String getTelephone() { 
     return telephone; 
    } 

    public void setTelephone(String telephone) { 
     this.telephone = telephone; 
    } 

    public String getEmail() { 
     return email; 
    } 

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

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public Date getDob() { 
     return dob; 
    } 

    public void setDob(Date dob) { 
     this.dob = dob; 
    } 
} 

@Repository パブリックインターフェイスUserRepositoryはPagingAndSortingRepository {

}

package rest.api.controller; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.hateoas.Resource; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.web.bind.annotation.*; 
import rest.api.data.repository.UserRepository; 
import rest.api.entity.User; 

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; 
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; 

@RestController 
@Service 
@Transactional 
public class UserController { 

    private final UserRepository userDao; 

    @Autowired 
    public UserController(UserRepository userDao) { 
     this.userDao = userDao; 
    } 

    @Transactional 
    @RequestMapping(value = "https://stackoverflow.com/users/{user-id}", method = RequestMethod.GET) 
    public ResponseEntity<Resource<User>> getSpecificModel(@PathVariable(value = "user-id") Long userId) { 


     User specificUser = userDao.findOne(userId); 

     Resource<User> resource = new Resource<>(specificUser, linkTo(methodOn(MessageController.class) 
       .getSpecificModel(userId)).withSelfRel()); 


     return ResponseEntity.ok(resource); 
    } 

    @Transactional 
    @RequestMapping(value = "https://stackoverflow.com/users/{user-id}", method = RequestMethod.DELETE) 
    public long remove(@PathVariable(value = "user-id") long userId) { 

     userDao.delete(userId); 
     return userId; 
    } 

    @RequestMapping(value = "/users", method = RequestMethod.POST) 
    public void registerLoginDetails(@RequestParam(name="username") String username, 
            @RequestParam(name="email") String email, 
            @RequestParam(name="password") String password) { 

     User loginDetails = new User(username, email, password); 

     userDao.save(loginDetails); 
    } 

    @Transactional 
    @RequestMapping(value = "/profile", method = RequestMethod.POST) 
    public void registerPersonalDetails() { 

    } 
} 

私のユーザーテーブルのすべての列がnullを取ることができ、そしてこの中でレコードを保存することが許可されているが拡張します状態。私は手動でdatabseに対して同等のクエリを実行することでこれをチェックしました。

誰にもアイデアはありますか?

答えて

1

一つまぶしいエラーは、ユーザーを登録するために使用さUserでコンストラクタは、渡されたusernameemailpasswordパラメータを割り当てていないということである。

+0

おっと、偶然の先頭にロングIDを追加しましたアイブようです私はコンストラクタを使用することになっていました。なぜ私も空の3引数のコンストラクタを持っていることは考えられません。よく目撃された!ありがとう! – gezinspace

関連する問題