我得到以下代码:
Car.java:
package org.tjatte;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Car")
public class Car {
@Id
@Column(name = "Car_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int carId;
@Column(name = "Car_DESIGNATION", nullable = false)
private String designation;
@ManyToOne
@JoinColumn(name = "GARAGE_ID")
public Garage carGarage;
public Car(String bez) {
this.designation = bez;
}
public int getCarId() {
return carId;
}
public void setCarId(int carId) {
this.carId = carId;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Garage getCarGarage() {
return carGarage;
}
public void setCarGarage(Garage carGarage) {
this.carGarage = carGarage;
}
public void print() {
System.out.println("Designation: " + getDesignation());
}
}
Garage.java
package org.tjatte;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "Garage")
public class Garage {
@Id
@Column(name = "Garage_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int garageId;
@Column(name = "Garage_DESIGNATION", nullable = false)
private String designation;
@OneToMany(mappedBy = "carGarage", cascade = CascadeType.ALL)
@Column(name = "GARAGE_ID")
public List<Car> carsInGarage = new ArrayList<Car>();
public Garage(String bez) {
this.designation = bez;
}
public int getGarageId() {
return garageId;
}
public void setGarageId(int garageId) {
this.garageId = garageId;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public List<Car> getCarsInGarage() {
return carsInGarage;
}
public void setCarsInGarage(List<Car> carsInGarage) {
this.carsInGarage = carsInGarage;
}
public void print() {
for(Car c : carsInGarage) {
c.print();
}
}
}
MainTest.java
package org.tjatte;
import java.util.Arrays;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class MainTest {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Garage garage = new Garage("Big");
Car oneCar = new Car("Opel");
Car anotherCar = new Car("Mercedes");
garage.setCarsInGarage(Arrays.asList(oneCar, anotherCar));
session.save(garage);
tx.commit();
}
}
我正在尝试实现Car和Garage之间的OneToMany映射,并且通常不会出现任何错误。但是最终结果不是我所期望的。
(图片中的数字1是已插入表Garage中的车库的ID。这就是确切的车库,我已在MainTest中添加了汽车)
我非常感谢您的帮助!花一些时间在上面,但是我看不到为什么我在那一栏上没有输入空值。
谢谢。