hibernate中Modle中的有些屬性不想建立表是在數據庫中產生字段保存數據,當這種需求是咱們能夠設置@transient表示透明的當設置此屬性是在建立數據庫是能夠對此屬性忽略,在本例中模擬了一個班級表表名爲MyClass 屬性有數據庫id 班級名稱 班級老師 老師身份證號 咱們的需求想把老師身份證號不保存到數據庫裏不想對身份證不持久化數據解決方法很簡單就是在對應的字段上面加@Transient的註解就搞定。java
實例代碼以下:mysql
第一步:建立Java工程編寫三個項目包把hibernate用的jar包添加到path裏而後建立Modle類代碼以下sql
package com.ygc.hibernate.modle; import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name="Class") //設置數據庫表名爲class public class MyClass implements Serializable { private int id; private String className; private String teacher; private String teacherNumber; @Id @GeneratedValue(strategy=GenerationType.AUTO) //設置主鍵自動增加 public int getId() { return id; } public void setId(int id) { this.id = id; } @Basic public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } //這是此註解後該屬性不會數據持久化也是本例要說明的註解 @Transient public String getTeacherNumber() { return teacherNumber; } public void setTeacherNumber(String teacherNumber) { this.teacherNumber = teacherNumber; } }
第二步:編寫測試類就是Main方法的。數據庫
package com.ygc.hibernate.main; import org.hibernate.Session; import com.ygc.hibernate.modle.MyClass; import com.ygc.hibernate.utils.HibernateUtils; public class ClassTest { /** * @param args */ public static void main(String[] args) { MyClass clas1 = new MyClass(); clas1.setClassName("大學二年級"); clas1.setTeacher("田紅菊"); clas1.setTeacherNumber("0100"); Session session = HibernateUtils.getFactory().openSession(); session.beginTransaction(); session.save(clas1); session.getTransaction().commit(); session.close(); HibernateUtils.getFactory().close(); } }
第三步:建立工具類就是把SessionFactory的方法單獨寫成工具類apache
package com.ygc.hibernate.utils; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; public class HibernateUtils { private HibernateUtils(){} private static HibernateUtils hibernateUtils; private HibernateUtils getHibernateUtils(){ if(hibernateUtils==null){ hibernateUtils = new HibernateUtils(); } return hibernateUtils; } public static SessionFactory getFactory(){ Configuration configuration = new AnnotationConfiguration().configure(); return configuration.buildSessionFactory(); } }
第四步:修改hibernate.cfg.xml的配置文件session
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) --> <!--<property name="connection.pool_size">1</property>--> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <!--<property name="current_session_context_class">thread</property>--> <!-- Disable the second-level cache --> <!--<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>--> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!--自動建立表--> <property name="hbm2ddl.auto">create</property> <!-- Drop and re-create the database schema on startup --> <!--<property name="hbm2ddl.auto">update</property>--> <!--<mapping resource="com/ygc/hibernate/modle/Students.hbm.xml"/>--> <mapping class="com.ygc.hibernate.modle.Class"/> </session-factory> </hibernate-configuration>
第五步:添加log4j的配置文件就是用來打印日誌的,記得添加log4j的jar包app
# Configure logging for testing: optionally with log file # debug,info,warn,error,fatal log4j.rootLogger=debug, stdout, logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=D:/logs/hibernate.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n log4j.logger.org.hibernate.tool.hbm2ddl=debug
最後一步運行查看結果ide
[sql] view plain copy工具
[sql] view plain copy
[sql] view plain copy
[sql] view plain copy
經過以上結果發現老師的身份證號碼沒有被插入到數據庫,說明咱們添加的@transient有效了。