如何使用 jpa 将一个简单的 class(不是实体)映射到 dto(Entity) class
How to map one simple class(not an entity) with dto(Entity) class using jpa
我尝试使用非@Entity class 进行@OneToOne 映射,但似乎我做错了什么请帮忙。这是以下 classes.
的错误日志
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.package.model.Hotel.address references an unknown entity: com.package.model.Address
这是我的Address.java
private String street;
private String city;
private String state;
private int pincode;
private String country;
private String locationCoordinates;
//Getters and setters removed from the code
这是我的Hotel.java
@Id
@GeneratedValue
private int id;
private String password;
private String name;
private String contactPerson;
private String email;
private String countryCode;
private long phone1;
private long phone2;
@OneToOne(cascade=CascadeType.ALL)
private Address address;
private String description;
地址 class 不是@Entity class 只有酒店 class 有@Entity。现在我正在尝试使用 spring 和 JPA insert/create(table) 值为 Address.java 的数据到单个酒店 table,我在 Eclipse 控制台中的抱怨越来越多。
这是我试图从中获取数据的 RestController
@Autowired
private RoomRepository roomRepository;
//RoomRepository is Interface which is extending JpaRepository
@GetMapping("hotels")
public List<Room> retriveAllHotels(){
return roomRepository.findAll();
}
就这样:
- 将地址 class 标记为 @Embeddable
- 将link标记为酒店地址class作为@Embedded
- 删除@OneToOne
地址字段将成为酒店实体的一部分
我尝试使用非@Entity class 进行@OneToOne 映射,但似乎我做错了什么请帮忙。这是以下 classes.
的错误日志Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.package.model.Hotel.address references an unknown entity: com.package.model.Address
这是我的Address.java
private String street;
private String city;
private String state;
private int pincode;
private String country;
private String locationCoordinates;
//Getters and setters removed from the code
这是我的Hotel.java
@Id
@GeneratedValue
private int id;
private String password;
private String name;
private String contactPerson;
private String email;
private String countryCode;
private long phone1;
private long phone2;
@OneToOne(cascade=CascadeType.ALL)
private Address address;
private String description;
地址 class 不是@Entity class 只有酒店 class 有@Entity。现在我正在尝试使用 spring 和 JPA insert/create(table) 值为 Address.java 的数据到单个酒店 table,我在 Eclipse 控制台中的抱怨越来越多。
这是我试图从中获取数据的 RestController
@Autowired
private RoomRepository roomRepository;
//RoomRepository is Interface which is extending JpaRepository
@GetMapping("hotels")
public List<Room> retriveAllHotels(){
return roomRepository.findAll();
}
就这样:
- 将地址 class 标记为 @Embeddable
- 将link标记为酒店地址class作为@Embedded
- 删除@OneToOne
地址字段将成为酒店实体的一部分