i got the following code:
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();
}
}
I am trying to realise a OneToMany mapping between Car and Garage, and in general there arent occurring any errors. But the final result is not what i have exptected.
(The number 1 on the picture is the ID of the garage, that has been inserted in the table Garage. Thats the exact garage, where i have added the cars in the MainTest)
I appreciate any kind of help! Spend hours on this but i cant see why i am getting null on that column.
Thanks.