我是为简单的rest api创建的代码,并且我配置了两个实体类的多对多关系,并且我想知道如何编写addContract方法并同时填充以下与其相关的Room和Contractroom表是我的代码,我需要一些帮助。
合同类别
@Entity
@Table(name = "T475_Contract")
public class Contract implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "contractId")
private long contractId;
@Column(name="start_date")
private Date st_date;
@Column(name="end_date")
private Date end_date;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "hotel_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
/*@JsonIgnore*/
private Hotel hotel;
@OneToMany(
mappedBy = "contract",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<ContractRoom> rooms = new ArrayList<>();
public Contract(){}
public Contract(Date st_date, Date end_date) {
this.st_date = st_date;
this.end_date = end_date;
}
//getters and setters
public void addRoom(Room room, int roomCount, double roomPrice){
ContractRoom contractRoom = new ContractRoom(this,room, roomCount, roomPrice);
rooms.add(contractRoom);
room.getContracts().add(contractRoom);
}
public void removeRoom(Room room){
for(Iterator<ContractRoom> iterator = rooms.iterator();
iterator.hasNext();
){
ContractRoom contractRoom = iterator.next();
if(contractRoom.getContract().equals(this) && contractRoom.getRoom().equals(room)){
iterator.remove();
contractRoom.getRoom().getContracts().remove(contractRoom);
contractRoom.setContract(null);
contractRoom.setRoom(null);
}
}
}
}
我的房间
@Entity
@Table(name="T475_Room")
@NaturalIdCache
@Cache(
usage = CacheConcurrencyStrategy.READ_WRITE
)
public class Room implements Serializable {
@Id
@NaturalId
@Column(name="room_type")
private String room_type;
@Column(name="max_adults")
private int max_adults;
/*@Column(name="room_count")
private int room_count;
@Column(name="room_price")
private double room_price;*/
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "hotel_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
/*@JsonIgnore*/
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private Hotel hotel;
@OneToMany(
mappedBy = "room",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<ContractRoom> contracts = new ArrayList<>();
public Room(){}
public Room(String room_type, int max_adults) {
this.room_type = room_type;
this.max_adults = max_adults;
}
}
我的ContractRoom类
@Entity
@Table(name = "T475_ContractRoom")
public class ContractRoom {
@EmbeddedId
private ContractRoomId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("contractId")
private Contract contract;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("room_type")
private Room room;
@Column(name="room_count")
private int room_count;
@Column(name="room_price")
private double room_price;
private ContractRoom() {}
public ContractRoom(Contract contract, Room room, int room_count, double room_price) {
this.contract = contract;
this.room = room;
this.room_count = room_count;
this.room_price = room_price;
this.id = new ContractRoomId(contract.getContractId(),room.getRoom_type());
}
//Getters and setters
}
我想编写将合同保存在服务类中具有相关contractRoom和room对象的数据库中的方法,该怎么写呢?
最佳答案
您必须创建一个Contracts存储库,在服务类中声明它,并使用spring数据中的方法.save(contract)
。