L lawliet
L lawliet
JCHJava Community | Help. Code. Learn.
Created by L lawliet on 11/9/2024 in #java-help
Doubt in hibernate
so i created these 2 simple entities package src.main.webapp.Entity;
import lombok.Data;
import src.main.webapp.Embeddable.EmployeeName;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "employee")
@Data
public class EmployeeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long salary;

public EmployeeEntity(Long salary, EmployeeName name, List<LaptopEntity> laptops) {
this.salary = salary;
this.name = name;
this.laptops = laptops;

}
private EmployeeName name;
@OneToMany
private List<LaptopEntity> laptops;
public EmployeeEntity(){}
public EmployeeEntity(Long salary, EmployeeName name) {
this.salary = salary;
this.name = name;
}
}
import lombok.Data;
import src.main.webapp.Embeddable.EmployeeName;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "employee")
@Data
public class EmployeeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long salary;

public EmployeeEntity(Long salary, EmployeeName name, List<LaptopEntity> laptops) {
this.salary = salary;
this.name = name;
this.laptops = laptops;

}
private EmployeeName name;
@OneToMany
private List<LaptopEntity> laptops;
public EmployeeEntity(){}
public EmployeeEntity(Long salary, EmployeeName name) {
this.salary = salary;
this.name = name;
}
}
package src.main.webapp.Entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "laptop")
@NoArgsConstructor
@ToString
@Data
public class LaptopEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long lid;
private String lname;
@ManyToOne
private EmployeeEntity employee;
public LaptopEntity(String lname) {
this.lname = lname;
}
}
package src.main.webapp.Entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "laptop")
@NoArgsConstructor
@ToString
@Data
public class LaptopEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long lid;
private String lname;
@ManyToOne
private EmployeeEntity employee;
public LaptopEntity(String lname) {
this.lname = lname;
}
}
In case i use a third join table i dont have to explicitly mention cascade to all where as if i use a foregin key in the manyToOne table i have to use it. why exactly?
4 replies