@OneToOne 注释中的外键问题

Problem with Foreign Key in @OneToOne annotation

我花了几天时间解决一个奇怪的问题。 Whosebug 中有大量类似的帖子,我已经检查了很多,但我无法找到与此类似的内容。我有一个 Human Parent class

import javax.persistence.*;
import java.util.Date;

@Entity(name = "Human")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Human
{
    @Id
    @Column(name = "human_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @JsonProperty("name")
    private String name;

    @JsonProperty("date_of_birth")
    private Date dateOfBirth;

    @Embedded
    @JsonProperty("authentication_credential")
    private AuthenticationCredentials authCred;

    // constructors + setter/getters
}

A User child class :

import javax.persistence.*;
import java.util.Date;

@Entity(name = "User")
@Table(name = "end_user")
public class User extends Human
{
    @OneToOne(mappedBy = "user",
              cascade = CascadeType.ALL,
              fetch = FetchType.LAZY,
              optional = false)
    @JsonProperty("contact_numbers")
    private CollectionOfContactMethods contactNumbers;

    @JsonProperty("telephone")
    private String telephone;

    // constructors + setter/getters
 }

还有一个CollectionOfContactMethodsUser有组成关系。

import javax.persistence.*;
import java.util.List;

@Entity(name = "CollectionOfContactMethods")
@Table(name = "collection_of_contact_methods")
public class CollectionOfContactMethods
{
    @Id
    @Column(name = "collection_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ext_human_id", referencedColumnName = "human_id")
    private User user;

    @JsonProperty("email")
    private String email;

    // constructors + setter/getters
}

计划,根据ER图是这样的:

但是,我在数据库级别得到的是这样的:

感谢您抽出时间。谢谢。

PS : 这是 UserController

@RestController
@RequestMapping("api/v1/user/")
public class UserController
{
    @Autowired
    UserService userService;

    @GetMapping(value = "users")
    Iterable<User> list()
    {
        Iterable<User> retVal = userService.findAll();
        return retVal;
    }

    @RequestMapping(value = "user", method = RequestMethod.POST)
    User create(@RequestBody User user)
    {
        return userService.save(user);
    }

    @RequestMapping(value = "user/{id}",  method = RequestMethod.GET )
    Optional<User> get(@PathVariable Long id)
    {
        return userService.findById(id);
    }
}

还有UserService :

import com.tsakirogf.schedu.model.human.User;
import org.springframework.data.repository.CrudRepository;

public interface UserService extends CrudRepository<User, Long>
{
}

由于 UserCollectionOfContactMethods 之间存在双向关系,问题可能是 CollectionOfContactMethods user 在您尝试创建它时为 null,因此数据库中的空数据或 not-null property references a null or transient value : com.tsakirogf.schedu.model.CollectionOfContactMethods.user 错误。尝试以下操作:

@RequestMapping(value = "user", method = RequestMethod.POST)
User create(@RequestBody User user) {
    CollectionOfContactMethods contactNumbers = user.getContactNumbers();
    contactNumbers.setUser(user);
    user.setContactNumbers(contactNumbers);
    return userService.save(user);
}