java - Spring JPA 映射 - 第一步

标签 java spring hibernate jpa

我有类(class)用户:

@Entity
public class User {

@Id
@GeneratedValue
private Integer id;
private String name;
private String password;

@ManyToMany
@JoinTable
private List<Role> roles;
}

类所有者继承自用户

@Entity
public class Owner extends User {

    private String pesel;
    private String adress;

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.REMOVE})
    private List<Pet> pets;
}

并且主人有宠物

public class Pet {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String weight;

    @ManyToOne
    private Owner owner;
}

为什么启动应用程序时出现错误:

org.springframework.data.mapping.PropertyReferenceException: No property user found for type Pet!

--编辑 首先我有版本,如下: enter image description here

现在我尝试将 User 实例分享给医生和动物的主人

enter image description here

问题是我不知道我是否正在做映射,因此想问它是否必须看起来像

--编辑2

我稍微简化了该方案,以更好地说明发生的情况

enter image description here

--编辑3

目前我的对象已呈现:

@Entity
public class Pet {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;
    private String weight;
}

用户

@Entity
public class User {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    private String password;

    @ManyToMany
    @JoinTable(name="user_roles")
    private List<Role> roles;

}

宠物主人

@Entity
public class PetOwner extends User {

    private String pesel;
    private String adress;

    @OneToMany(mappedBy="petOwner")
    private List<Pet> pets;
}

最佳答案

我替换

@ManyToOne
    private PetOwner petOwner;

对于

 @ManyToOne
        private Owner petOwner;

并且它有效。你有 PetOwner 类(class)吗?

还提供日志错误以获取有关它的更多信息

关于java - Spring JPA 映射 - 第一步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35394002/

相关文章:

java - Activity 之间的转换不正确

java - 导航栏中的 wicket-bootstrap 面包屑

java - MySQLIntegrityConstraintViolationException :Duplicate entry exception for @JoinTable in hibernate

java - int 方法返回错误值

JavaMail Yandex 发件人

java - 如何修复 "Field ... required a bean of type ... that could not be found"异常 Spring Boot

java - 在 OneToMany 的情况下不会发生数据库插入

java - 使用 Spring Batch 和 Spring Cloud Data Flow 构建文件轮询/摄取任务

hibernate - 无法捕获由违反约束引起的 hibernate 异常

java - 如何从可用作自定义实体 ID 生成器中实体 ID 的对象属性生成 UID?