大发体育娱乐在线-大发体育娱乐官方网站-大发体育娱乐登录网址
做最好的网站

集合映射

来源:http://www.dfwstonefabricators.com 作者:大发体育网络 人气:53 发布时间:2019-11-04
摘要:@Index用法——javax.persistence.Index,@index package com.springup.utiku.model;import java.io.Serializable;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.per

@Index用法——javax.persistence.Index,@index

package com.springup.utiku.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;

@Entity
@Table(indexes = { 
        @Index(name="studentAnswerQueIdIndex", columnList="studentAnswerQueId DESC"),
        @Index(name="questionConIdIndex", columnList="questionConId") 
        })
public class StudentAnswerConEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private Long studentAnswerQueId;
    private Long questionConId;
    private String selected;// 学员答题记录
    public StudentAnswerConEntity() {}
    public StudentAnswerConEntity(Long studentAnswerQueId, Long questionConId) {
        this.studentAnswerQueId = studentAnswerQueId;
        this.questionConId = questionConId;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Long getQuestionConId() {
        return questionConId;
    }
    public void setQuestionConId(Long questionConId) {
        this.questionConId = questionConId;
    }
    public Long getStudentAnswerQueId() {
        return studentAnswerQueId;
    }
    public void setStudentAnswerQueId(Long studentAnswerQueId) {
        this.studentAnswerQueId = studentAnswerQueId;
    }
    public String getSelected() {
        return selected;
    }
    public void setSelected(String selected) {
        this.selected = selected;
    }
}

 

package com.springup.utiku.model; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Generate...

前几日改了一整日到底搞出来了,用的打注解的法子,用部门和职工间的涉及关系做例子(打申明实乃比写xml方便多了....卡塔尔。
多个机构有三个职工。
代码如下
1.创立机关和职工的实体类,并打上注解
(1)员工类
package dayentity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="tb_emp")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name")
private String name;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
}

(2)部门类
package dayentity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="tb_dept")
public class Dept {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private  int dno;
private String name;
@OneToMany
@Column(name="id")
private Set<Employee> employee=new HashSet<>();
public int getDno() {
    return dno;
}
public void setDno(int dno) {
    this.dno = dno;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public Set<Employee> getEmployee() {
    return employee;
}
public void setEmployee(Set<Employee> employee) {
    this.employee = employee;
}

}

(3卡塔 尔(英语:State of Qatar)记得把类文件加载到hibernate.cfg.xml文件中,然后写测量试验类
package dayentity;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class test0001 {
public static void main(String[] args) {

    Configuration cfg=new Configuration().configure();
    SessionFactory sessionFactory=cfg.buildSessionFactory();
    Session session=sessionFactory.openSession();
    Transaction ts=session.beginTransaction();

    Dept dept=new Dept();
    dept.setDno(111);
    dept.setName("锤锤");

Employee emp=new Employee();
emp.setId(11);
emp.setName("lsj");
dept.getEmployee().add(emp);
session.save(dept);
session.save(emp);
ts.commit();
session.close();
}

}

末段应该有三张表

....放不了图..就不看了

补偿有些 级联操作 cascade 应该写在set配置里

有七种接受 all 全体操作都进行级联操作
save_update 独有在保存和更新时才开展级联操作
delete 只有在剔除时举行级联操作
none 全体操作都不实行级联操作

补偿一下 用写配置文件的议程做一些多映射
(1卡塔尔写实体类
部门类
package day31;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

public class Dept {

private  int dno;
private String name;

private Set<Employee> employee=new HashSet<>();

public int getDno() {
    return dno;
}
public void setDno(int dno) {
    this.dno = dno;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public Set<Employee> getEmployee() {
    return employee;
}
public void setEmployee(Set<Employee> employee) {
    this.employee = employee;
}

}
员工类
package day31;

public class Employee {

private int id;

private String name;


public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}
(2卡塔尔国配置文件
部门类
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="day31">

<class name="Dept" table="dept">
    <id name="dno" column="dno" type="integer">
        <generator class="native"/>
    </id>
    <property name="name"/>
    <set name="employee" table="employee">
    <key column="dno"></key>
    <one-to-many class="day31.Employee"/>
    </set>

</class>

</hibernate-mapping>
员工类

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="day31">

<class name="Employee" table="employee">
    <id name="id" column="id" type="integer">
        <generator class="native"/>
    </id>
    <property name="name"/>

</class>

</hibernate-mapping>
(3)
测试类
package day31;

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

public class test0001 {
public static void main(String[] args) {

    Configuration cfg=new Configuration().configure();
    SessionFactory sessionFactory=cfg.buildSessionFactory();
    Session session=sessionFactory.openSession();
    Transaction ts=session.beginTransaction();

    Dept dept=new Dept();
    dept.setDno(112);
    dept.setName("song");


Employee emp=new Employee();

emp.setId(12);
emp.setName("dudu");
dept.getEmployee().add(emp);
session.save(dept);
session.save(emp);
ts.commit();
session.close();

}

}

本文由大发体育娱乐在线发布于大发体育网络,转载请注明出处:集合映射

关键词:

上一篇:消除Ubunut下Cacti不出图的主题材料

下一篇:没有了

最火资讯