论坛首页 Java企业应用论坛

Hiberate Annotations如何配置嵌入式组件(embedded component)

浏览 2708 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2008-03-05  
Hiberate Annotations如何配置嵌入式组件(embedded component)


我想用Hiberate Annotations 配置嵌入式组,但怎么改也不对,报异常,望大哥们帮小弟看一下.指点指点.谢谢了.

1.我的表结构
create table PERSONAL
(
  ID NUMBER not null,
  NAME VARCHAR2(20),
  HOME_STREET VARCHAR2(255),
  HOME_CITY VARCHAR2(255),
  HOME_PROVINCE VARCHAR2(255),
  HOME_ZIPCODE VARCHAR2(255),
  COM_STREET VARCHAR2(255),
  COM_CITY VARCHAR2(255),
  COM_PROVINCE VARCHAR2(255),
  COM_ZIPCODE VARCHAR2(255)
)

alter table PERSONAL add constraint PK_PERSONAL_ID_1 primary key (ID)


2.Personal.java
@SuppressWarnings("serial")
@Entity
@Table(name = "Personal")
@SequenceGenerator(name = "PERSONAL_SEQ", sequenceName = "PERSONAL_SEQ")
public class Personal implements Serializable {
private int id;
private String name;

@Embedded
@AttributeOverrides({
@AttributeOverride(name="street",column=@Column(name="home_street")),
@AttributeOverride(name="city",column=@Column(name="home_city")),
@AttributeOverride(name="province",column=@Column(name="home_province")),
@AttributeOverride(name="zipcode",column=@Column(name="home_zipcode"))
})
private Address homeAddress;

@Embedded
@AttributeOverrides({
@AttributeOverride(name="street",column=@Column(name="com_street")),
@AttributeOverride(name="city",column=@Column(name="com_city")),
@AttributeOverride(name="province",column=@Column(name="com_province")),
@AttributeOverride(name="zipcode",column=@Column(name="com_zipcode"))
})
private Address comAddress;

public Personal(){}

public Personal(String name,Address homeAddress,Address comAddress){
this.name = name;
this.homeAddress = homeAddress;
this.comAddress = comAddress;
}

public Address getComAddress() {
return comAddress;
}

public void setComAddress(Address comAddress) {
this.comAddress = comAddress;
}

public Address getHomeAddress() {
return homeAddress;
}

public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}

@Id
@GeneratedValue(generator="PERSONAL_SEQ")
public int getId() {
return id;
}

@SuppressWarnings("unused")
private void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


3.Address.java
@SuppressWarnings("serial")
@Embeddable
public class Address implements Serializable {
private String street;
private String city;
private String province;
private String zipcode;

public Address() {
  // TODO Auto-generated constructor stub
}

public Address(String street,String city,String province,String zipcode){
  this.street = street;
  this.city = city;
  this.province = province;
  this.zipcode = zipcode;
}

@Column(name="city",insertable=false, updatable=false)
public String getCity() {
  return city;
}
public void setCity(String city) {
  this.city = city;
}

@Column(name="province",insertable=false, updatable=false)
public String getProvince() {
  return province;
}
public void setProvince(String province) {
  this.province = province;
}
@Column(name="street",insertable=false, updatable=false)
public String getStreet() {
  return street;
}
public void setStreet(String street) {
  this.street = street;
}
@Column(name="zipcode",insertable=false, updatable=false)
public String getZipcode() {
  return zipcode;
}
public void setZipcode(String zipcode) {
  this.zipcode = zipcode;
}
}

   发表时间:2008-03-05  
真是郁闷哪.这个问题困扰了我几个小时,问题已经找到,原来是我自己马虎了,对不起大家了.
@Embedded
@AttributeOverrides({
@AttributeOverride(name="street",column=@Column(name="com_street")),
@AttributeOverride(name="city",column=@Column(name="com_city")),
@AttributeOverride(name="province",column=@Column(name="com_province")),
@AttributeOverride(name="zipcode",column=@Column(name="com_zipcode"))
})
private Address comAddress;
这段代码应该写在get方法上面.

希望在研究Hibernate Annotations的朋友,一起研究,共同学习.
我的QQ:45001700 
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics