Hibernate OneToOne映射行为取决于ManyToMany映射

时间:2018-07-01 17:02:17

标签: hibernate many-to-many one-to-one

标题

我正在学习Hibernate,为了练习,我开发了一个程序。

但是,似乎OneToOne映射的行为取决于ManyToMany映射。

以下是我的代码:-

应用程序类

data1

用户类别

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(UserSupp.class).addAnnotatedClass(Users_X_Address.class).addAnnotatedClass(Courses.class).buildSessionFactory();
        Session session = factory.getCurrentSession();

        Users userOne = new Users();
        userOne.setName("UserOne");

        Users userTwo = new Users();
        userTwo.setName("UserTwo");

        UserSupp suppOne = new UserSupp();
        suppOne.setSuppField("SuppOne");

        UserSupp suppTwo = new UserSupp();
        suppTwo.setSuppField("SuppTwo");

        session.beginTransaction();

        Users_X_Address addOne = new Users_X_Address();
        Users_X_Address addTwo = new Users_X_Address();
        Users_X_Address addThree = new Users_X_Address();
        Users_X_Address addFour = new Users_X_Address();

        addOne.setAddress("Address One");
        addTwo.setAddress("Address Two");

        addThree.setAddress("Address Three");
        addFour.setAddress("Address Four");

        userOne.setSupp(suppOne);
        userTwo.setSupp(suppTwo);
        suppOne.setUser(userOne);
        suppTwo.setUser(userTwo);

        userOne.addAddress(addOne);
        userOne.addAddress(addTwo);

        userTwo.addAddress(addThree);
        userTwo.addAddress(addFour);









        Courses courseOne = new Courses();
        Courses courseTwo = new Courses();
        Courses courseThree = new Courses();
        Courses courseFour = new Courses();

        courseOne.setCourse_name("Course One");
        courseTwo.setCourse_name("Course Two");
        courseThree.setCourse_name("Course Three");
        courseFour.setCourse_name("Course Four");

        userOne.addCousre(courseOne);
        userOne.addCousre(courseTwo);
        userOne.addCousre(courseThree);       // **Line 1**
        userTwo.addCousre(courseThree);
        userTwo.addCousre(courseFour);

        session.save(userOne);
        session.save(userTwo);

        session.getTransaction().commit();




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

UserSupp类

   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.ManyToMany;
    import javax.persistence.OneToMany;
    import javax.persistence.OneToOne;

    @Entity
    public class Users
    {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        int userId;
        String name;
        @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
        private UserSupp supp;

        @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
        private List<Users_X_Address> address;

        @ManyToMany(mappedBy = "user", cascade = CascadeType.ALL)
        private List<Courses> course;

        public List<Users_X_Address> getAddress() {
            return address;
        }

        public void setAddress(List<Users_X_Address> address) {
            this.address = address;
        }

    public List<Courses> getCourse() {
        return course;
    }

    public void setCourse(List<Courses> course) {
        this.course = course;
    }

    public void addAddress(Users_X_Address addr)
    {
        if(address == null)
            address = new ArrayList<Users_X_Address>();

            address.add(addr);
            addr.setUser(this);

    }

    public void addCousre(Courses cours)
    {
        if(course == null)
            course = new ArrayList<Courses>();

            course.add(cours);
            cours.addUser(this);

    }

    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 UserSupp getSupp() {
        return supp;
    }
    public void setSupp(UserSupp supp) {
        this.supp = supp;
    }
}

} **

User_X_Address类

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.OneToOne;
@Entity
public class UserSupp
{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int suppId;
String suppField;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private Users user;
public Users getUser() {
    return user;
}
public void setUser(Users user) {
    this.user = user;
}
public UserSupp() {
}
public int getSuppId() {
    return suppId;
}
public void setSuppId(int suppId) {
    this.suppId = suppId;
}
public String getSuppField() {
    return suppField;
}
public void setSuppField(String suppField) {
    this.suppField = suppField;
}

课程

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 Users_X_Address
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int address_id;
    String address;
    @ManyToOne(targetEntity = Users.class, cascade = CascadeType.ALL)
    @JoinColumn(name = "address_userid")
    private Users user;
    public int getAddress_id() {
        return address_id;
    }
    public void setAddress_id(int address_id) {
        this.address_id = address_id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Users getUser() {
        return user;
    }
    public void setUser(Users user) {
        this.user = user;
    }
    public Users_X_Address() {
        super();
    }


}

案例1:-在应用类中未注释第1行

表usersupp中的数据库值是:-

suppId-suppField-user_id

1-SuppTwo-2

2-SuppOne-1

案例2:-在应用类中带有注释行1

表usersupp中的数据库值是:-

suppId-suppField-user_id

1-SuppTwo-2

2-SuppOne-2

问题:-是否可以解释在CASE 1中为什么在DB中插入了错误的条目?

0 个答案:

没有答案