Created
March 13, 2025 01:07
-
-
Save hakkm/5fd7adfa43b360913008a3ed5c0f1984 to your computer and use it in GitHub Desktop.
One-to-Many bidirectional: no join table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hello.demo.model; | |
import jakarta.persistence.*; | |
import jakarta.persistence.Entity; | |
import jakarta.persistence.GeneratedValue; | |
import jakarta.persistence.GenerationType; | |
import jakarta.persistence.Id; | |
@Entity | |
public class Laptop { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
private String name; | |
@ManyToOne | |
@JoinColumn(name = "user_id") | |
private User user; | |
public Laptop(String name, User user) { | |
this.name = name; | |
this.user = user; | |
} | |
public Laptop() { | |
} | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public User getUser() { | |
return user; | |
} | |
public void setUser(User user) { | |
this.user = user; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Laptop(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "Laptop{" + | |
"id=" + id + | |
", name='" + name + '\'' + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hello.demo.model; | |
import jakarta.persistence.*; | |
import java.util.List; | |
@Entity | |
@Table(name = "app_user") | |
public class User { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
private String fullName; | |
@OneToMany(mappedBy = "user",cascade = CascadeType.ALL) | |
private List<Laptop> laptop; | |
public User() { | |
} | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getFullName() { | |
return fullName; | |
} | |
public void setFullName(String fullName) { | |
this.fullName = fullName; | |
} | |
public List<Laptop> getLaptop() { | |
return laptop; | |
} | |
public void setLaptop(List<Laptop> laptop) { | |
this.laptop = laptop; | |
} | |
public User(String fullName, List<Laptop> laptop) { | |
this.fullName = fullName; | |
this.laptop = laptop; | |
} | |
@Override | |
public String toString() { | |
return "User{" + | |
"id=" + id + | |
", fullName='" + fullName + '\'' + | |
", laptop=" + laptop + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment