hibernate关联关系-组件关联

组件关联关系的映射与原理:

 

模型:User — Name(两者同属一张表中)

 

package Domain;

import java.util.Date;

public class User {
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Name getName() {
		return name;
	}
	public void setName(Name name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	private int id;
	// private String name;
	private Name name;
	private Date birthday;
	
}

 

 

package Domain;

public class Name {	
	
	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;
	}
	
	private String firstName;
	private String lastName;
}

 

 

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

	<class name="User" table="user">
		<id name="id">
			<generator class="native" />
		</id>
		<!-- 
		<property name="name" unique="true"/>
		 -->
		<component name="name">
			<property name="firstName" column="first_name"/>
			<property name="lastName" column="last_name"/>
		</component>
		<property name="birthday"/>
	</class>
</hibernate-mapping>

 

 

package Dao.Test;

import java.util.Date;

import Dao.UserDAO;
import Dao.Impl.UserDAOImpl;
import Domain.Name;
import Domain.User;

public class UserDAOTest {
	public static void main(String[] args) {
		/*UserDAO dao = new UserDAOImpl();
		User user = new User();
		user.setName("Eric");
		user.setBirthday(new Date());
		System.out.println("==============================================");
		dao.saveUser(user);
		
		System.out.println("==============================================");
		user.setName("new Eric");
		dao.updateUser(user);
		
		System.out.println("==============================================");
		User u = dao.findUserByName(user.getName());
		System.out.println("==============================================");
		dao.removeUser(u);*/
		
		User user = new User();
		user.setBirthday(new Date());
		Name n = new Name();
		n.setFirstName("firstName");
		n.setLastName("lastName");
		user.setName(n);
		
		UserDAO dao = new UserDAOImpl();
		dao.saveUser(user);
		
	}
}

 

你可能感兴趣的:(DAO,Hibernate,.net,xml)