我正在尝试使用Hibernate在 Oracle数据库中存储几个对象。但是,我似乎无法正确地建立映射关系。就目前而言,我收到了 NullPointerException 。
最后,每个 Client 应包含 ID,姓氏,电子邮件和一组预订。
每个航班应包含航班号,出发时间和出发机场。
预订应具有 ID,客户,航班,预订日期和预订座位数。
目前,我只是在一个客户端上对其进行测试,以免使其过于复杂,但我感到相当困惑。
这是我到目前为止所做的:
主要
import java.sql.Date;
import java.sql.Time;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAApplication {
private EntityManagerFactory entityManagerFactory;
public JPAApplication() {
Logger.getLogger("org.hibernate").setLevel(Level.ALL);
entityManagerFactory = Persistence.createEntityManagerFactory("DB1");
}
public void testFlights() {
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Client firstClient = new Client("Kiro", "Betona", "kirobetona@gmail.com");
em.persist(firstClient);
}
public static void main(String[] args) {
JPAApplication app = new JPAApplication();
app.testFlights();
}
}
客户
import java.util.*;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
public class Client {
private int id;
private String firstName;
private String lastName;
private String email;
private Set<Booking> reservations;
Client(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setId(int id) {
this.id = id;
}
@OneToMany(targetEntity = Booking.class, mappedBy="client", fetch=FetchType.EAGER)
public Set<Booking> getReservations() {
return reservations;
}
public void setReservations(Set<Booking> reservations) {
this.reservations = reservations;
}
}
预订
import java.util.Date;
import javax.persistence.*;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
@Entity
public class Booking {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private Client client;
private Flight flight;
private int bookedSeats = 1;
@Temporal(TemporalType.DATE)
private Date bookingDate;
Booking(Client client, Flight flight, Date bookingDate) {
this.client = client;
this.flight = flight;
this.bookingDate = bookingDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="id")
public Client getClient() {
return client;
}
@OneToOne
@PrimaryKeyJoinColumn(name="flight")
public Flight getFlight() {
return flight;
}
public int getBookedSeats() {
return bookedSeats;
}
public void setBookedSeats(int bookedSeats) {
this.bookedSeats = bookedSeats;
}
public Date getBookingDate() {
return bookingDate;
}
public void setBookingDate(Date bookingDate) {
this.bookingDate = bookingDate;
}
public void setClient(Client client) {
this.client = client;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
}
飞行
import java.sql.Time;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
public class Flight {
private String flightNumber;
private Time startingTime;
private String startingAirport;
Flight(String flightNumber, Time startingTime, String startingAirport) {
this.flightNumber = flightNumber;
this.startingTime = startingTime;
this.startingAirport = startingAirport;
}
@Id
@OneToOne(targetEntity = Booking.class, mappedBy="flight", fetch=FetchType.EAGER)
@PrimaryKeyJoinColumn(name="flight")
public String getFlightNumber() {
return flightNumber;
}
public Time getStartingTime() {
return startingTime;
}
public void setStartingTime(Time startingTime) {
this.startingTime = startingTime;
}
public String getStartingAirport() {
return startingAirport;
}
public void setStartingAirport(String startingAirport) {
this.startingAirport = startingAirport;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
}
答案 0 :(得分:0)
当您在Booking中使用@JoinColumn批注时,我会感到有些困惑:您要让Booking使用名为“ id”的列作为客户端的外键,但是与Booking主键冲突。
您可以改为将列名称分开,如下所示:
@JoinColumn(name =“ clientId”)
另外,对于Hibernate实例化Entity对象,实体should have a no-arg constructor。在示例代码中,默认的no-arg构造函数已被覆盖。