Hibernate必知必会——持久化Set

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010871004/article/details/81973861

Set

相比于List而言,Set给我们提供的数据结构是无序的,不可重复的。Set是一个接口,通常下面的实现类有java.util.HashSet, java.util.
LinkedHashSet等。

持久化Set

@Entity
@Table(name = "employee")
public class Employee {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private long id;
    @Column(name = "name")
    private String name;
    @ElementCollection
    @CollectionTable(name = "email")
    private Set<String> emails;

    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 Set<String> getEmails() {
        return emails;
    }

    public void setEmails(Set<String> emails) {
        this.emails = emails;
    }

    @Override
    public String toString() {
        return "Employee" + "\n\tId: " + this.id + "\n\tName: " + this.name + "\n\tEmails: " + this.emails;
    }
}

插入记录

SessionFactory sessionFactory =
HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Employee employee = new Employee();
employee.setName("yogesh");
Set<String> emails = new HashSet<String>();
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
employee.setEmails(emails);
session.getTransaction().begin();
session.save(employee);
session.getTransaction().commit();

打印出来的sql记录

Hibernate: insert into employee (name) values (?)
Hibernate: insert into email (Employee_id, emails) values (?, ?)
Hibernate: insert into email (Employee_id, emails) values (?, ?)
Hibernate: insert into email (Employee_id, emails) values (?, ?)

查询记录

Employee employee = (Employee) session.get(Employee.class, 1l);
System.out.println(employee.toString());

sql输出

Hibernate: select employee0_.id as id0_0_, employee0_.name as
name0_0_ from employee employee0_ where employee0_.id=?
Hibernate: select emails0_.Employee_id as Employee1_0_0_,
emails0_.emails as emails0_ from email emails0_ where
emails0_.Employee_id=?
Employee
Id: 1
Name: yogesh
Emails: [[email protected].com,
[email protected].com, [email protected].com]

更新记录

Employee employee = (Employee) session.get(Employee.class, 1l);
Set<String> emails = employee.getEmails();
emails.add("[email protected]");
session.getTransaction().begin();
session.saveOrUpdate(employee);
session.getTransaction().commit();
System.out.println(employee.toString());

sql输出

Hibernate: select employee0_.id as id0_0_, employee0_.name as
name0_0_ from employee employee0_ where employee0_.id=?
Hibernate: select emails0_.Employee_id as Employee1_0_0_,
emails0_.emails as emails0_ from email emails0_ where
emails0_.Employee_id=?
Employee
Id: 1
Name: yogesh
Emails: [[email protected].com,
[email protected].com [email protected].com]

删除记录

Employee employee = new Employee();
employee.setId(1);
session.getTransaction().begin();
session.delete(employee);
session.getTransaction().commit();

sql输出

Hibernate: delete from email where Employee_id=?
Hibernate: delete from employee where id=?

猜你喜欢

转载自blog.csdn.net/u010871004/article/details/81973861
0条评论
添加一条新回复