#Doubt in hibernate

3 messages · Page 1 of 1 (latest)

storm niche
#

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;
    }
}

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?

chrome birchBOT
#

This post has been reserved for your question.

Hey @storm niche! 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.