组件映射

阅读更多

表结构:

CREATE TABLE `mapping`.`student` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `address` VARCHAR(45) NULL,
  `idNumber` VARCHAR(45) NOT NULL,
  `deposit` DOUBLE NULL,
  PRIMARY KEY (`id`));

 

Student 类

 

package hibernate.entities;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Student {
	private int id;
	private String name;
	private String address;
	private StudentCard card;
	
	@Id
	@GeneratedValue
	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;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	@Embedded
	@AttributeOverrides({
		@AttributeOverride(name="idNumber", column=@Column(name="idNumber")),
		@AttributeOverride(name="deposit", column=@Column(name="deposit"))
	}
	)
	public StudentCard getCard() {
		return card;
	}
	public void setCard(StudentCard card) {
		this.card = card;
	}
	
	
}

 

 

StudentCard 类

package hibernate.entities;

import javax.persistence.Embeddable;

@Embeddable
public class StudentCard {
	private String idNumber;
	private double deposit;
	
	public String getIdNumber() {
		return idNumber;
	}
	public void setIdNumber(String idNumber) {
		this.idNumber = idNumber;
	}
	public double getDeposit() {
		return deposit;
	}
	public void setDeposit(double deposit) {
		this.deposit = deposit;
	}
	
	
}

 

你可能感兴趣的:(组件映射)