gpt4 book ai didi

java - OneToOne 映射中的 MappedBy

转载 作者:行者123 更新时间:2023-12-01 18:26:45 26 4
gpt4 key购买 nike

我创建了三个类 - User、UserSubscription 和 Subscription。我希望 UserSubscription 表中的用户应由 User 表中现有的用户引用。那么,如何使用mappedBy呢?类别如下:

用户:

    package com.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

@Entity
public class User {


private int userId;
private String password;
private String contact;
private String firstName;
private String lastName;
private String gender;
private String address;
private int age;
private String email;
private String countBooks;


private Set<Language> languages = new HashSet<Language>(0);
private Set<Book> favorites = new HashSet<Book>(0);


@Id
@GeneratedValue
@Column(name="User_Id")
public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

@Column(name="Password")
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}


@Column(name="Contact")
public String getContact() {
return contact;
}

public void setContact(String contact) {
this.contact = contact;
}


@Column(name="First_Name")
public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}


@Column(name="Last_Name")
public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}


@Column(name="Gender")
public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}


@Column(name="Address")
public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}


@Column(name="Age")
public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}


@Column(name="Email")
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}


@Column(name="Count_Books")
public String getCountBooks() {
return countBooks;
}

public void setCountBooks(String countBooks) {
this.countBooks = countBooks;
}

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="User_Language", joinColumns = {
@JoinColumn(name = "User_Id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "Language_Id",
nullable = false, updatable = false) })
public Set<Language> getLanguages() {
return languages;
}

public void setLanguages(Set<Language> languages) {
this.languages = languages;
}

@ManyToMany
@JoinTable(name="User_Favorite", joinColumns = {
@JoinColumn(name = "User_Id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "Book_Id",
nullable = false, updatable = false) })
public Set<Book> getFavorites() {
return favorites;
}

public void setFavorites(Set<Book> favorites) {
this.favorites = favorites;
}

}

用户订阅:

package com.model;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class UserSubscription {

private int userSubscriptionId;
private Subscription subscription;
private User user;
private Date subscriptionStartDate;
private Date subscriptionEndDate;
private int status;


@Id
@GeneratedValue
@Column(name="User_Subscription_Id")
public int getUserSubscriptionId() {
return userSubscriptionId;
}
public void setUserSubscriptionId(int userSubscriptionId) {
this.userSubscriptionId = userSubscriptionId;
}

@Column(name="Subscription_Id")
public Subscription getSubscription() {
return subscription;
}
public void setSubscription(Subscription subscription) {
this.subscription = subscription;
}
@OneToOne(mappedBy="userId")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}

@Column(name="Subscription_Start_Date")
public Date getSubscriptionStartDate() {
return subscriptionStartDate;
}
public void setSubscriptionStartDate(Date subscriptionStartDate) {
this.subscriptionStartDate = subscriptionStartDate;
}

@Column(name="Subscription_End_Date")
public Date getSubscriptionEndDate() {
return subscriptionEndDate;
}
public void setSubscriptionEndDate(Date subscriptionEndDate) {
this.subscriptionEndDate = subscriptionEndDate;
}

@Column(name="Status")
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}





}

订阅:

package com.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Subscription {


private int subscriptionId;
private String name;
private int period;
private float fees;
private int noOfBooks;
private int Status;

@Id
@GeneratedValue
public int getSubscriptionId() {
return subscriptionId;
}
public void setSubscriptionId(int subscriptionId) {
this.subscriptionId = subscriptionId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public float getFees() {
return fees;
}
public void setFees(float fees) {
this.fees = fees;
}
public int getNoOfBooks() {
return noOfBooks;
}
public void setNoOfBooks(int noOfBooks) {
this.noOfBooks = noOfBooks;
}
public int getStatus() {
return Status;
}
public void setStatus(int status) {
Status = status;
}



}

执行时出现异常:

Exception in thread "main" org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: com.model.User.userId in mappedBy of com.model.UserSubscription.user
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:248)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at com.impetus.model.Check.main(Check.java:92)

最佳答案

在您的代码中,user-user_subscription 看起来是单向的。 'mappedby' 应该用于双向关系。

您可以在用户实体中使用以下代码使其与 user_subscription 双向。

private UserSubscription userSubscription; 
@OneToOne
public User getUserSubscription() {
return userSubscription;
}

或者从user_subscription中删除mappedby并使其成为单向。

关于java - OneToOne 映射中的 MappedBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25860571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com