我有一個名為userPrimary key的資料庫表id。我還有另一個名為的資料庫表user_profile,其主鍵為user_id. 我希望該user_id列與我的用戶表中的主鍵鏈接,id但不知道該怎么做。我知道它涉及制作一個與主鍵鏈接的外鍵,并且它與@JoinTableor有關@JoinColumn,但不知道該怎么做。如果有人可以幫助我,我將不勝感激。
UserProfile.java:
import net.bytebuddy.dynamic.loading.InjectionClassLoader;
import org.springframework.web.multipart.MultipartFile;
import javax.persistence.*;
@Entity
@Table(name = "user_profile")
public class UserProfile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long id;
// @OneToOne
// @JoinColumn(table = "users", name = "username")
//// @PrimaryKeyJoinColumn(name = "profile_id")
// User user;
@Column(name = "profile_img")
private String profile_img;
@Column(name = "profile_banner")
private String profile_banner;
@Column(name = "user_alias")
private String user_alias;
//
// public void setUsers(User users) {
// this.user = users;
// }
//
// public User getUser() {
// return user;
// }
public Long getProfileId() {
return id;
}
public void setProfileId(Long id) {
this.id = id;
}
public String getProfile_img() {
return profile_img;
}
public String getProfile_img_complete() {
return profile_img;
}
public void setProfile_img(String profile_img) {
this.profile_img = profile_img;
}
public String getProfile_banner() {
return profile_banner;
}
public String getProfile_banner_complete() {
return profile_banner;
}
public void setProfile_banner(String profile_banner) {
this.profile_banner = profile_banner;
}
public String getUser_alias() {
return user_alias;
}
public void setUser_alias(String user_alias) {
this.user_alias = user_alias;
}
}
User.java:
import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.HashSet;
import java.util.Set;
import com.user.UserProfile;
@Entity
@Table( name = "users",
uniqueConstraints = {
@UniqueConstraint(columnNames = "username")
})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(max = 50)
private String username;
@NotBlank
@Size(max = 120)
private String password;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable( name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
//
// @OneToOne(mappedBy = "email")
// UserProfile userProfile;
// public Long getId() {
// return id;
// }
//
// public void setId(Long id) {
// this.id = id;
// }
//
// public UserProfile getUserProfile() {
// return userProfile;
// }
//
// public void setUserProfile(UserProfile userProfile) {
// this.userProfile = userProfile;
// }
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
uj5u.com熱心網友回復:
訣竅是在里面的參考上使用@MapsId。這會將您的主鍵列映射到.UserUserProfileUser idUserProfile user_id
User保持不變:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// skipped code
}
該UserProfile id列不需要@GeneratedValue,因為 id 將填充User物體的 id。Hibernate 將期望user_id在表中有一列user_profile:
@Entity
@Table(name = "user_profile")
public class UserProfile {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private User user;
// skipped code
}
除了user_id在user_profiletable 中命名列之外,您還可以將其命名為id,但要做到這一點,您需要JoinColumn(name = "id")在User參考中添加 a :
@Entity
@Table(name = "user_profile")
public class UserProfile {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
@JoinColumn(name = "id")
private User user;
// skipped code
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448890.html
上一篇:如何將外部陣列傳??遞給查詢?
