Hibernate抱怨@OneToOne中的@Id为空,即使它不为空 [英] Hibernate complains for null @Id in @OneToOne even if it is not null

查看:586
本文介绍了Hibernate抱怨@OneToOne中的@Id为空,即使它不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在CarPerson之间使用相同的@Id来实现@OneToOne关联. Person是可选的Car,但是Car具有必需的Person(因此,我需要在"cars"表内指向现有Person的外键):

I am trying to achieve @OneToOne association, using the same @Id between a Car and a Person. A Person is has an optional Car, but a Car has a required Person (hence I need a foreign key inside "cars" table pointing an existing Person):

@Entity
@Table(name = "persons")
public class Person {

    @Id
    @Column(name = "name")
    private String name;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "person")
    private Car car;

    /**
     * For hibernate
     */
    @SuppressWarnings("unused")
    private Person() {
    }

    public Person(String name) {
        super();
        this.name = name;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String getName() {
        return name;
    }

}


@Entity
@Table(name = "cars")
public class Car {
    @Id
    @Column(name = "name")
    private String name;

    @MapsId
    @OneToOne
    @JoinColumn(name = "name")
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Person person;

    public Car() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

}

public interface PersonRepository extends CrudRepository<Person, String> {

}

我的测试用例:

@Autowired
private PersonRepository personRepository;

@Test
public void test() {
    Person mike = new Person("Mike");
    personRepository.save(mike); //Saved sucessfully

    Person alice = new Person("Alice");
    Car car = new Car();
    car.setPerson(alice);
    car.setName(alice.getName());
    alice.setCar(car);
    personRepository.save(alice); //error
}

Stacktrace:

Stacktrace:

org.springframework.orm.jpa.JpaSystemException:尝试分配ID 来自空的一对一属性[com.project.entity.Car.person]; 嵌套的异常是org.hibernate.id.IdentifierGenerationException: 尝试从空的一对一属性分配ID [com.project.entity.Car.person]

org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property [com.project.entity.Car.person]; nested exception is org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.project.entity.Car.person]

我遵循了此解决方案,了解如何使用共享ID.数据库中的架构看起来很好,并且完全可以按照我的要求工作.如果我删除汽车,则什么也不会发生.如果我删除一个拥有Car的人,那么他的Car也将从数据库中删除.

I followed this solution on how to have a shared Id. The schema in database looks to be fine, and works exactly as I want. If i delete a Car, nothing happens. If I delete a person who owns a Car, his Car will be deleted from database as well.

问题很简单. 我该怎么办?

万一它发挥了作用(我对此表示怀疑),这是我的数据源属性:

In case it plays any role (I doubt it), this is my datasource properties:

spring.jpa.hibernate.dll-auto = create-drop
spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

推荐答案

(免责声明:这并不意味着是Q& A)

(Disclaimer: It was not meant to be a Q&A)

问题出在@MapsId批注中.更改为:

The problem is in @MapsId annotation. Changing it to:

@MapsId("name") //Here
@OneToOne
@JoinColumn(name = "name")
@OnDelete(action = OnDeleteAction.CASCADE)
private Person person;

似乎可以解决问题.

这篇关于Hibernate抱怨@OneToOne中的@Id为空,即使它不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆