How to map one simple class(not an entity) with dt

2019-08-17 15:10发布

I tried @OneToOne Mapping with non @Entity class but it seems i am doing something wrong please help. Here is error log for the following classes.

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.package.model.Hotel.address references an unknown entity: com.package.model.Address

Here is my 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

Here is my 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;

Address class is not @Entity class Only Hotel class have @Entity. Now i am trying to insert/create(table) data with the value of Address.java using spring and JPA into a single hotel table, And i am getting above complain in my Eclipse console.

Here is my RestController from where i am trying to get data

@Autowired
private RoomRepository roomRepository;
//RoomRepository is Interface which is extending JpaRepository

@GetMapping("hotels")
public List<Room> retriveAllHotels(){
    return roomRepository.findAll();
}

1条回答
倾城 Initia
2楼-- · 2019-08-17 15:42

Just do:

  1. mark Address class as @Embeddable
  2. mark link to Address in Hotel class as @Embedded
  3. delete @OneToOne

and the Address fields will be a part of a Hotel entity

查看更多
登录 后发表回答