我创建了三个类-用户,用户订阅和订阅。我希望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中删除mapbyby并使其成为单向的。

10-06 09:49