hibernate学习

hibernate一对多关联:

    一对多关联有两种方式:一是主键关联,二是惟一外键关联。
    外键关联:
    public class Person {

private Long id;
private int age;
private String firstname;
private String lastname;

public Person(){}

public Long getId() {
return id;
}

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

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}
}

  <hibernate-mapping package="com.daotang.test.domain">
  <class name="Person" table="PERSON">
       <id name="id" column="PERSON_ID">
            <generator class="native"> </generator>
       </id>
       <property name="age" column="AGE" type="int"></property>
        <property name="firstname" column="FIRSTNAME" type="string"></property>
         <property name="lastname" column="LASTNAME" type="string"></property>
  </class>
</hibernate-mapping>


import java.util.Date;

public class Event {

private Long id;
private String title;
private Date sdate;

private Person person;
public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}

public Event(){}

public Long getId() {
return id;
}

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

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Date getSdate() {
return sdate;
}

public void setSdate(Date sdate) {
this.sdate = sdate;
}

}



<hibernate-mapping package="com.daotang.test.domain">
  <class name="Event" table="EVENT">
     <id name="id" column="EVENT_ID">
         <generator class="native"></generator>
     </id>
     <property name="title" column="TITLE" type="string"></property>
     <property name="sdate" column="SDATE" type="date"></property>
      <many-to-one name="person" class="Person" column="PERSON_ID" unique="true"></many-to-one>       </class>
</hibernate-mapping> 

主键关联:

public class Person {

private Long id;
private int age;
private String firstname;
private String lastname;

private Event event;
}

<hibernate-mapping package="com.daotang.test.domain">
  <class name="Person" table="PERSON">
       <id name="id" column="PERSON_ID">
           <generator class="foreign">
                <param name="property">event</param>
            </generator>
       </id>
       <property name="age" column="AGE" type="int"></property>
        <property name="firstname" column="FIRSTNAME" type="string"></property>
         <property name="lastname" column="LASTNAME" type="string"></property>
         <one-to-one name="event" class="Event" constrained="true"></one-to-one>
  </class>
</hibernate-mapping>

public class Event {

private Long id;
private String title;
private Date sdate;
}

<hibernate-mapping package="com.daotang.test.domain">
  <class name="Event" table="EVENT">
     <id name="id" column="EVENT_ID">
         <generator class="native"></generator>
     </id>
     <property name="title" column="TITLE" type="string"></property>
     <property name="sdate" column="SDATE" type="date"></property>
  </class>
</hibernate-mapping>

主键关联就是共享一个主键。

你可能感兴趣的:(Hibernate)