第1页共22页Hibernate注解编程!--EJB注解编程--hibernate-configurationsession-factorypropertyname=hibernate.connection.urljdbc:mysql://localhost:3306/test/propertypropertyname=hibernate.connection.driver_classcom.mysql.jdbc.Driver/propertypropertyname=hibernate.connection.usernameroot/propertypropertyname=hibernate.connection.passwordimti/propertypropertyname=hibernate.dialectorg.hibernate.dialect.MySQLDialect/propertymappingclass=com.xmxz.hibernate.annotation.IdCard/mappingclass=com.xmxz.hibernate.annotation.Person//session-factory/hibernate-configuration!—XML映射--hibernate-configurationsession-factorypropertyname=hibernate.connection.urljdbc:mysql://localhost:3306/test/propertypropertyname=hibernate.connection.driver_classcom.mysql.jdbc.Driver/propertypropertyname=hibernate.connection.usernameroot/propertypropertyname=hibernate.connection.passwordimti/propertypropertyname=hibernate.dialectorg.hibernate.dialect.MySQLDialect/propertypropertyname=hibernate.show_sqltrue/propertypropertyname=hibernate.hbm2ddl.autoupdate/propertymappingresource=com/xmxz/hibernate/mobile/Operator.hbm.xml/mappingresource=com/xmxz/hibernate/mobile/MobileUser.hbm.xml/mappingresource=com/xmxz/hibernate/mobile/FillMoneyCard.hbm.xml/mappingresource=com/xmxz/hibernate/mobile/SPConst.hbm.xml/mappingresource=com/xmxz/hibernate/mobile/MobileSP.hbm.xml/mappingresource=com/xmxz/hibernate/mobile/FillMoneyHistory.hbm.xml/mappingresource=com/xmxz/hibernate/mobile/Message.hbm.xml//session-factory/hibernate-configuration第2页共22页ExportDBpublicclassExportDB{publicstaticvoidmain(String[]args){//读取hibernate.cfg.xml文件Configurationcfg=newConfiguration().configure();SchemaExportexport=newSchemaExport(cfg);export.create(true,true);}}HibernateUtilspublicclassHibernateUtils{privatestaticSessionFactoryfactory;static{//单例模式try{Configurationcfg=newConfiguration().configure();factory=cfg.buildSessionFactory();}catch(HibernateExceptione){e.printStackTrace();}}/***获取当前的会话工厂*@return*/publicstaticSessionFactorygetSessionFactory(){returnfactory;}/***获取当前的会话*@return*/publicstaticSessiongetSession(){returnfactory.openSession();}/***关闭会话(session)*@paramsession当前的会话*/publicstaticvoidcloseSession(Sessionsession){if(session!=null){if(session.isOpen()){session.close();}}}}第3页共22页ExportDBAnnotationpublicclassExportDBAnnotation{publicstaticvoidmain(String[]args){//读取hibernate.cfg.annotation.xml文件Configurationcfg=newAnnotationConfiguration().configure(hibernate.cfg.annotation.xml);SchemaExportexport=newSchemaExport(cfg);//export.setFormat(true);//设置保存SQL脚本的文件名//export.setOutputFile(new.sql);//输出SQL脚本,并执行SQL脚本export.create(true,true);}}HibernateAnnotationUtils/***使用EJB注解做的*@authorAdministrator*/publicclassHibernateAnnotationUtils{privatestaticSessionFactoryfactory;static{//单例模式try{Configurationcfg=newAnnotationConfiguration().configure(hibernate.cfg.annotation.xml);factory=cfg.buildSessionFactory();}catch(HibernateExceptione){e.printStackTrace();}}publicstaticSessionFactorygetSessionFactory(){returnfactory;}publicstaticSessiongetSession(){returnfactory.openSession();}publicstaticvoidcloseSession(Sessionsession){if(session!=null){if(session.isOpen()){session.close();}}}}第4页共22页【一对一】关系映射模型:人和身份证通过人可以得知身份证号码,根据身份证号码可以得知人Person/***人(通过外键实现一对一关系)*@authorAdministrator*///屏蔽缺少“serialVersionUID”定义警告@SuppressWarnings(serial)//指明实体Bean名,默认值是类名@Entity(name=Person)//实体对应的数据表name表名;uniqueConstraints唯一约束性;主键@Table(name=t_person,uniqueConstraints={@UniqueConstraint(columnNames={id})})publicclassPersonimplementsSerializable{privateIntegerid;privateStringname;privateIdCardidCard;@Id//主键唯一标识@Column(name=id)//字段名@GeneratedValue(strategy=GenerationType.AUTO)//自动增加publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}@Column(name=name,length=16,nullable=false)//name字段名,length字段最大字符长度,nullable是否允许为空,precision字段精度,scale小数位数publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}//optional是否允许该字段为null,该属性应该根据数据库表的外键约束来确定,默认为true@JoinColumn(name=cardID)@OneToOne(cascade=CascadeType.ALL,optional=false)publicIdCardgetIdCard(){returnidCard;}publicvoidsetIdCard(IdCardidCard){this.idCard=idCard;}}第5页共22页IdCard/***身份证(通过外键实现一对一关系)*@authorAdministrator*///屏蔽缺少“serialVersionUID”定义警告@SuppressWarnings(serial)//指明实体Bean名,默认值是类名@Entity(name=IdCard)//实体对应的数据表name表名;uniqueConstraints唯一约束性;主键@Table(name=t_idcard,uniqueConstraints={@UniqueConstraint(columnNames={id})})publicclassIdCardimplementsSerializable{privateIntegerid;privateStringcardNo;privatePersonperson;@Id//主键唯一标识@Column(name=id)//字段名@GeneratedValue(strategy=GenerationType.AUTO)//自动增加publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}@Column(name=cardNo,length=32,nullable=false)//name字段名,length字段最大字符长度,nullable是否允许为空,precision字段精度,scale小数位数publicStringgetCardNo(){returncardNo;}publicvoidsetCardNo(StringcardNo){this.cardNo=cardNo;}//表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性@OneToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE},mappedBy