Hibernate ManyToOne 不同的行为

Hibernate ManyToOne different behavior

我有两个用户对象和四个课程对象。

案例 1:-(我理解) 将编号为 1 和 2 的课程对象映射到编号为 1 的用户对象,并将编号为 3 和 4 的课程对象映射到编号为 2 的用户对象。 数据库中的条目:-

用户table:- userid 名称 detailsid 1个1 2 两个 2

课程 table :-

courseid 课程名 userid 1 课程一 1 2 课程二 1 3 课程三 2 4 课程四 2

案例 2:-(不同的行为) 将编号为 1 和 2 的课程对象映射到编号为 1 的用户对象,并将编号为 2,3 和 4 的课程对象映射到编号为 2 的用户对象。 数据库中的条目:-

用户table:- userid 名称 detailsid 1个1 2 两个 2

课程 table :-

courseid 课程名 userid 1 课程一 1 2 课程二 2 3 课程三 2 4 课程四 2

问题:- 为什么课程对象编号 2 也没有与用户对象编号 1 一起插入?我希望案例 2 的课程 table 中的条目如下所示“-

课程 table :-

courseid 课程名 userid 1 课程一 1 2 课程二 1 2 课程二 2 3 课程三 2 4 课程四 2

问题似乎是 courseId 是主要的,第一个条目被第二个条目覆盖,如果是这样,那么如何处理这种情况?

代码:-

主要class(应用程序)

    import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class App
{
    public static void main(String[] args)
    {
        SessionFactory factory = new Configuration().configure().addAnnotatedClass(Users.class).addAnnotatedClass(UserDetails.class).addAnnotatedClass(Courses.class).buildSessionFactory();
        Session session = factory.getCurrentSession();

        Users userOne = new Users("One");
        Users userTwo = new Users("Two");

        UserDetails detailsOne = new UserDetails("detailOne");
        UserDetails detailsTwo = new UserDetails("detailTwo");

        Courses courseOne = new Courses("CourseOne");
        Courses courseTwo = new Courses("CourseTwo");
        Courses courseThree = new Courses("CourseThree");
        Courses courseFour = new Courses("CourseFour");

        userOne.setDetails(detailsOne);
        userTwo.setDetails(detailsTwo);

        userOne.addCourse(courseOne);
        userOne.addCourse(courseTwo);
        userTwo.addCourse(courseThree);
        userTwo.addCourse(courseFour);
        //userTwo.addCourse(courseOne);

        session.beginTransaction();
        session.save(userOne);
        session.save(userTwo);
        session.getTransaction().commit();

        session.close();
        factory.close();
    }
}

用户class:-

    import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Users
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int userid;
    String name;

    @OneToOne( cascade  = CascadeType.ALL)
    @JoinColumn(name = "details_id")
    private UserDetails details;

    @OneToMany(mappedBy = "user_course",cascade = CascadeType.ALL)
    private List<Courses> courseList;

    public void addCourse(Courses course)
    {
        if(courseList == null)
        {
            courseList  = new ArrayList<Courses>() ;
        }

        courseList.add(course);

        course.setUser_course(this);
    }

    public List<Courses> getCourseList() {
        return courseList;
    }

    public void setCourseList(List<Courses> courseList) {
        this.courseList = courseList;
    }

    public Users(String name) {
        this.name = name;
    }

    public Users() {
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public UserDetails getDetails() {
        return details;
    }

    public void setDetails(UserDetails details) {
        this.details = details;
    }
}

用户详细信息class:-

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

import org.hibernate.engine.internal.Cascade;

@Entity
public class UserDetails
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int detailsid;

    String location;

    @OneToOne(mappedBy = "details", cascade  = CascadeType.ALL)
    private Users user;


    public Users getUser() {
        return user;
    }



    public void setUser(Users user) {
        this.user = user;
    }



    public UserDetails() {
    }



    public UserDetails(String location) {
        this.location = location;
    }


    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

}

课程class:-

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Courses
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int course_id;
    String courseName;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "userId")
    private Users user_course;
    public String getCourseName() {
        return courseName;
    }
    public Courses(String courseName) {
        super();
        this.courseName = courseName;
    }
    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }
    public Users getUser_course() {
        return user_course;
    }
    public void setUser_course(Users user_course) {
        this.user_course = user_course;
    }
}

配置文件:-

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name = "connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name = "connection.url">jdbc:sqlserver://20.198.58.29:1433;instanceName=SQLEXPRESS;databaseName=DB</property>
        <property name = "connection.username">sa</property>
        <property name = "connection.password">deteam@123</property>
        <property name = "show_sql">true</property>
        <property name = "dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
        <property name = "connection.pool_size">1</property>
        <property name = "current_session_context_class">thread</property>
        <property name = "hibernate.hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

因为你的 class 'Courses'.

写错了
  1. class 有一个成员指向一门特定的课程。所以它不能指向两个不同的课程。从来没有。

  2. 您的 classes 和成员(变量)的命名不是 Java 标准的,对于习惯了标准的人来说很难理解。切勿对 class-名称使用复数形式。并始终为事物使用正确的名称。

这是错误的:private Users user_course;
这样会更好:private User user;