有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

@OneToOne注释中外键的java问题

我花了几天时间处理一个奇怪的问题。StackOverflow中有很多类似的帖子,我已经检查了很多,但是我找不到类似的帖子。我有一个Human父类

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
}

一个User子类:

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图,计划如下:

enter image description here

然而,我在数据库级别得到的是:

enter image description here

我很感激你花的时间。谢谢

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

还有{}:

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

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

共 (1) 个答案

  1. # 1 楼答案

    由于UserCollectionOfContactMethods之间存在双向关系,问题可能是CollectionOfContactMethods{}在尝试创建时为空,因此数据库中的空数据或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);
    }