首先是2张表
表A:
表B:
其中表B中的FormBaseId对应表A中的SubjectID. 数据库中没有设置外键关系。
下面是2个对应的实体
package questionnaire.model; /** * AbstractAdsubject entity provides the base persistence definition of the * Adsubject entity. @author MyEclipse Persistence Tools */ public abstract class AbstractAdsubject implements java.io.Serializable { // Fields private Integer adSubjectId; //private Integer formBaseId; private String advertisingType; private String advertisingContext; private Subject subject;//这里去掉表B中的formBaseId字段,改用实体 // Constructors public Subject getSubject() { return subject; } public void setSubject(Subject subject) { this.subject = subject; } /** default constructor */ public AbstractAdsubject() { } /** full constructor */ public AbstractAdsubject(Subject subject,String advertisingType, String advertisingContext) { this.subject = subject; //this.formBaseId = formBaseId; this.advertisingType = advertisingType; this.advertisingContext = advertisingContext; } // Property accessors public Integer getAdSubjectId() { return this.adSubjectId; } public void setAdSubjectId(Integer adSubjectId) { this.adSubjectId = adSubjectId; } /* public Integer getFormBaseId() { return this.formBaseId; } public void setFormBaseId(Integer formBaseId) { this.formBaseId = formBaseId; } */ public String getAdvertisingType() { return this.advertisingType; } public void setAdvertisingType(String advertisingType) { this.advertisingType = advertisingType; } public String getAdvertisingContext() { return this.advertisingContext; } public void setAdvertisingContext(String advertisingContext) { this.advertisingContext = advertisingContext; } }
package questionnaire.model; import java.sql.Timestamp; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import questionnaire.util.TimestampAdapter; /** * AbstractSubject entity provides the base persistence definition of the * Subject entity. @author MyEclipse Persistence Tools */ public abstract class AbstractSubject implements java.io.Serializable { // Fields private Integer subjectId; private String name; private Integer points; private Integer copies; private String formType; private String thumbnail; private String description; private Timestamp updateTime; private Boolean isTop; private Boolean isValid; private Integer sex; private Integer companyId; private Integer startAge; private Integer endAge; // Constructors /** default constructor */ public AbstractSubject() { } /** minimal constructor */ public AbstractSubject(String name, Integer copies, Timestamp updateTime, Boolean isTop, Boolean isValid) { this.name = name; this.copies = copies; this.updateTime = updateTime; this.isTop = isTop; this.isValid = isValid; } /** full constructor */ public AbstractSubject(String name, Integer points, Integer copies, String formType, String thumbnail, String description, Timestamp updateTime, Boolean isTop, Boolean isValid, Integer sex, Integer companyId, Integer startAge, Integer endAge) { this.name = name; this.points = points; this.copies = copies; this.formType = formType; this.thumbnail = thumbnail; this.description = description; this.updateTime = updateTime; this.isTop = isTop; this.isValid = isValid; this.sex = sex; this.companyId = companyId; this.startAge = startAge; this.endAge = endAge; } // Property accessors public Integer getSubjectId() { return this.subjectId; } public void setSubjectId(Integer subjectId) { this.subjectId = subjectId; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Integer getPoints() { return this.points; } public void setPoints(Integer points) { this.points = points; } public Integer getCopies() { return this.copies; } public void setCopies(Integer copies) { this.copies = copies; } public String getFormType() { return this.formType; } public void setFormType(String formType) { this.formType = formType; } public String getThumbnail() { return this.thumbnail; } public void setThumbnail(String thumbnail) { this.thumbnail = thumbnail; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public Timestamp getUpdateTime() { return this.updateTime; }
//这里是为了Json的时候用Timestam类型在json的时候会序列化不出来 @XmlJavaTypeAdapter(value=TimestampAdapter.class,type=Timestamp.class) public void setUpdateTime(Timestamp updateTime) { this.updateTime = updateTime; } public Boolean getIsTop() { return this.isTop; } public void setIsTop(Boolean isTop) { this.isTop = isTop; } public Boolean getIsValid() { return this.isValid; } public void setIsValid(Boolean isValid) { this.isValid = isValid; } public Integer getSex() { return this.sex; } public void setSex(Integer sex) { this.sex = sex; } public Integer getCompanyId() { return this.companyId; } public void setCompanyId(Integer companyId) { this.companyId = companyId; } public Integer getStartAge() { return this.startAge; } public void setStartAge(Integer startAge) { this.startAge = startAge; } public Integer getEndAge() { return this.endAge; } public void setEndAge(Integer endAge) { this.endAge = endAge; } }
下面是为了json做转换的类
package questionnaire.util; import java.sql.Timestamp; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class TimestampAdapter extends XmlAdapter<Date, Timestamp> { public Date marshal(Timestamp v) { return new Date(v.getTime()); } public Timestamp unmarshal(Date v) { return new Timestamp( v.getTime()); } }
类 XmlAdapter<ValueType,BoundType>
java.lang.Object
javax.xml.bind.annotation.adapters.XmlAdapter<ValueType,BoundType>
- 类型参数:
BoundType
- JAXB 不知道如何处理的一些类型。编写一个适配器,以便允许通过 ValueType 将此类型用作内存表示形式。ValueType
- JAXB 无需其他操作便知道如何处理的类型。
在编组或解组过程中,由 JAXB 绑定框架调用这些方法:
- XmlAdapter.marshal(...):编组过程中,JAXB 绑定框架调用 XmlAdapter.marshal(..) 将 bound 类型修改为 value 类型,然后将 value 类型编组为 XML 表示形式。
- XmlAdapter.unmarshal(...):解组过程中,JAXB 绑定框架首先将 XML 表示形式解组为 value 类型,然后调用 XmlAdapter.unmarshal(..) 将 value 类型修改为 bound 类型。
下面是xml配置
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="questionnaire.model.Adsubject" table="adsubject" catalog="questionnaire"> <id name="adSubjectId" type="java.lang.Integer"> <column name="AdSubjectID" /> <generator class="identity" /> </id> <!-- <property name="formBaseId" type="java.lang.Integer"> <column name="FormBaseId" not-null="true"> <comment>表头åºç±»ID</comment> </column> </property> --> <property name="advertisingType" type="java.lang.String"> <column name="AdvertisingType" length="50" not-null="true"> <comment>广å表类å-å ³èDictionary</comment> </column> </property> <property name="advertisingContext" type="java.lang.String"> <column name="AdvertisingContext" length="500" not-null="true"> <comment>广å表å 容</comment> </column> </property>
<!--FormBaseId就是我们没有写的一个数据库字段 --> <many-to-one name="subject" class="questionnaire.model.Subject" column="FormBaseId" cascade="all" unique="true" /> </class> </hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="questionnaire.model.Subject" table="subject" catalog="questionnaire"> <id name="subjectId" type="java.lang.Integer"> <column name="SubjectID" /> <generator class="identity" /> </id> <property name="name" type="java.lang.String"> <column name="Name" length="50" not-null="true"> <comment>æ é¢</comment> </column> </property> <property name="points" type="java.lang.Integer"> <column name="Points"> <comment>积å</comment> </column> </property> <property name="copies" type="java.lang.Integer"> <column name="Copies" not-null="true"> <comment>å¯å¡«ä»½æ°</comment> </column> </property> <property name="formType" type="java.lang.String"> <column name="FormType" length="50"> <comment>1æ¯ADï¼2æ¯QU</comment> </column> </property> <property name="thumbnail" type="java.lang.String"> <column name="Thumbnail" length="500"> <comment>缩ç¥å¾</comment> </column> </property> <property name="description" type="java.lang.String"> <column name="Description" length="500"> <comment>æè¿°</comment> </column> </property> <property name="updateTime" type="java.sql.Timestamp"> <column name="UpdateTime" length="19" not-null="true"> <comment>æ´æ°æ¶é´</comment> </column> </property> <property name="isTop" type="java.lang.Boolean"> <column name="IsTop" not-null="true"> <comment>æ¯å¦ç½®é¡¶</comment> </column> </property> <property name="isValid" type="java.lang.Boolean"> <column name="IsValid" not-null="true"> <comment>æ¯å¦ææ</comment> </column> </property> <property name="sex" type="java.lang.Integer"> <column name="Sex"> <comment>æ§å« 1æ¯ç·ï¼2æ¯å¥³</comment> </column> </property> <property name="companyId" type="java.lang.Integer"> <column name="CompanyID"> <comment>å ¬å¸ID</comment> </column> </property> <property name="startAge" type="java.lang.Integer"> <column name="StartAge"> <comment>å¼å§å¹´é¾</comment> </column> </property> <property name="endAge" type="java.lang.Integer"> <column name="EndAge"> <comment>ç»æå¹´é¾</comment> </column> </property> </class> </hibernate-mapping>
最后是调用
@GET @Path("testOne2One") @Produces("application/json;charset=UTF-8") public String testOne2One() { try { Adsubject adsubject=new Adsubject(); adsubject.setAdvertisingContext("test"); adsubject.setAdvertisingType("2"); Subject subject=new Subject(); subject.setCompanyId(1); subject.setCopies(1); subject.setDescription("qq"); subject.setEndAge(1); subject.setFormType("2"); subject.setIsTop(true); subject.setIsValid(true); subject.setName("aa"); subject.setPoints(2); subject.setSex(1); subject.setStartAge(2); subject.setThumbnail("www.baidu.com");
//看见配置里面UpdateTime的长度为19,而date取出来的时间是带有毫秒的,会抛出could not insert错误,具体错误为Incorrect datetime value: '' for column 'UpdateTime' at row 1 subject.setUpdateTime(Timestamp.valueOf(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()))); adsubject.setSubject(subject); HibernateUtils.save(subject); HibernateUtils.save(adsubject); return "1"; } catch (RuntimeException e) { e.printStackTrace(); return "2"; // TODO: handle exception } }
hibernate输出来看,执行了3次数据操作。
Hibernate: insert into questionnaire.subject (Name, Points, Copies, FormType, Thumbnail, Description, UpdateTime, IsTop, IsValid, Sex, CompanyID, StartAge, EndAge) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into questionnaire.adsubject (AdvertisingType, AdvertisingContext, FormBaseId) values (?, ?, ?)
Hibernate: update questionnaire.subject set Name=?, Points=?, Copies=?, FormType=?, Thumbnail=?, Description=?, UpdateTime=?, IsTop=?, IsValid=?, Sex=?, CompanyID=?, StartAge=?, EndAge=? where SubjectID=?
数据库没有设置外键,表A于表B示怎么关联起来的呢。
<!--FormBaseId就是我们没有写的一个数据库字段 --> <many-to-one name="subject" class="questionnaire.model.Subject" column="FormBaseId" cascade="all" unique="true" />
这里只指定了表A对应的字段,表B中的SubjectId怎么与FormBaseId对应起来。
subject是嵌套类的字段名,class值位实体的位置,column="FormBaseId" 是当前实体中对应的数据库中的字段名。
这样写后hibernate会自己关联嵌套类的主键。
执行
HibernateUtils.save(subject);的时候
SubjectId已经返回到实体中去了。
在执行
subject