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?
1 Reply
JavaBot
JavaBot2w ago
This post has been reserved for your question.
Hey @L lawliet! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server