一对多关系休眠麻烦

时间:2015-11-09 02:37:36

标签: java spring hibernate spring-mvc

你好,我试图学习一对多的映射,但我真的遇到了hibernate的问题。我能够坚持使用数据库,但是当尝试应用一对多关系时,它不会持久存在于数据库中,并且在查看邮递员中的响应主体时也不会显示关系。从昨天早上起,我真的需要帮助解决这个问题。我已经在youtube和互联网上查看了教程,但每个教程看起来都是基本的,当应用相同的想法没有成功。我有一个实体人和另一个实体组织。一个人可以属于一个组织,但不同的人可以属于同一个组织。所以我的方法是使用一对多关系。

以下是我的组织实体:

@Entity
@Table(name="organization")
public class Organization {

    @Id
    @Column(name="org_Id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name, description;

    @OneToMany(/*fetch = FetchType.EAGER, cascade = CascadeType.ALL*/)
    @JoinTable(joinColumns = @JoinColumn(name="org_Id"),
    inverseJoinColumns = @JoinColumn(name="person_Id"))
   // @JsonIgnore

    //@JoinColumn(name="org_Id")
    private Collection<Person> personCollection = new ArrayList<Person>();

    public Collection<Person> getPersonCollection() {
        return personCollection;
    }

    public void setPersonCollection(Collection<Person> personCollection) {
        this.personCollection = personCollection;
    }

    private Address address;

    public Organization() {}

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Address getAddress() {
        return address;
    }

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

以下是我的Person实体:@Entity

@Table(name = "Person")
public class Person {
    @Id
    @Column(name="person_Id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name="Email",unique = true)
    private String email;
    @Column(name="FirstName")
    private String first_name;
    @Column(name="LastName")
    private String last_name;
    @Column(name="Description")
    private String description;

    //@Embedded
    private Address address;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JsonIgnore
    private Organization organization;

    public Organization getOrganization() {
      return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Person() {}


    public Address getAddress() {
        return address;
    }

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

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

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

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

试图创造人时。以下是我的创建方法:

public Person createPerson(String f_name, String l_name, String email, String city, String state,
                               String zipCode, String street, String description, Long id) {

        Person person = null;
        //f_name, l_name, email are required parameters if empty return null and throws an exception..
        if(f_name.isEmpty() || l_name.isEmpty() || email.isEmpty()) {

            return person;
        }
        else {
            Session session = null;
            Transaction transaction = null;
            try {
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();
                person = new Person();
                person.setFirst_name(f_name);
                person.setLast_name(l_name);
                person.setEmail(email);
                person.setDescription(description);
                Address address = new Address();
                address.setStreet(street);
                address.setZipCode(zipCode);
                address.setState(state);
                address.setCity(city);
                person.setAddress(address);
/* checks to see if id of organization exist if so add to list if not don't do anything.*/
                if(id!=null) {
                    Organization organization = session.get(Organization.class, id);
                    if (organization != null) {
     /* adds id of organization to person table and vice versa.*/
                        person.setOrganization(organization);
                        organization.getPersonCollection().add(person);
                    } else {
                        //do nothing
                    }
                }
                session.save(person);
                transaction.commit();
            } catch (HibernateException ex) {
                if (transaction != null)
                    transaction.rollback();
                ex.printStackTrace();
            } finally {
                if (session != null)
                    session.close();
            }
            return person;
        }
    }

我能够创建人员和组织,并坚持使用数据库。但是,当我尝试将组织添加到数据库中的人行时,我无法添加关系(当我尝试查找数据库本身时验证)并且也没有响应,因为我也得到了延迟初始化收集错误。请有人遇到过这个问题

1 个答案:

答案 0 :(得分:0)

我刚刚使用以下简化结构执行了你在hibernate中提供的代码片段,它完全正常。你应该从这里开始并根据你的需要进行修改。

Entity
@Table(name="organization")
public class Organization {

    @Id
    @Column(name="org_Id")
    private long id;
    private String name, description;

    @OneToMany(cascade = CascadeType.ALL)
    private Collection<Person> personCollection = new ArrayList<Person>();

    public Collection<Person> getPersonCollection() {
        return personCollection;
    }

    public void setPersonCollection(Collection<Person> personCollection) {
        this.personCollection = personCollection;
    }


    public Organization() {}

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

@Entity
@Table(name = "Person")
public class Person {
    @Id
    @Column(name="person_Id")
    private long id;
    @Column(name="Email",unique = true)
    private String email;
    @Column(name="FirstName")
    private String first_name;
    @Column(name="LastName")
    private String last_name;
    @Column(name="Description")
    private String description;

    @ManyToOne()
    private Organization organization;

    public Organization getOrganization() {
      return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public Person() {}

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

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

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

编辑:关系的人员方面的CascadeType.ALL已移至关系的组织方面。因为您希望在删除组织时,人员也应该被删除,但不是相反。