Spring Jpa - hibernate在数据库

时间:2016-06-25 16:21:23

标签: java spring hibernate jpa spring-boot

在spring boot 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 = "/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 = "/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() {

    }
}

我的users表中的每一列都可以为null,因此允许在此状态下保存记录。我通过手动对数据库执行等效查询来检查这一点。

有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

一个明显的错误是用于注册用户的User中的构造函数没有分配传入的usernameemailpassword参数。< / p>