-
映射類型查看全部
-
hibernate4.3的初始化 用于會話的獲取和關閉: public class HibernateUtil{ private static SessionFactory sessionFactory; private static Session session;} static{ //創建configuration對象,讀取hibernate.cfg.configure(); StandardsrciceRegistryBuilder ssrb=new SrandarsServlceRedistryBuilser() .applySettings(config.getProperties()); SrandardSerciceRegistry ssr=ssrb.build(); sessionFactory=config.buildSessionFactory(ssr); } //創建會話工廠sessionFactory獲取 public static SessionFactory getSessionFactory(){ return sessionFactory; } //獲取session public static Session getSession(){ session=sessionFactory.openSession(); return session; } //關閉session public static void closeSession(Sessionsession){ if(session!=null){ session.close(); } }查看全部
-
cascade屬性查看全部
-
set元素的常用屬性查看全部
-
級聯操作cascade屬性查看全部
-
cascade進行級聯操作,減少代碼量查看全部
-
inverse可以提升性能查看全部
-
一定要在班級中初始化private Set<Student> students = new HashSet<Student>(); 不然報空指針異常查看全部
-
java-hibernate-標準sql類型對應表查看全部
-
實現單向一對多: 在one方的實體中添加保存many方的集合 在one放的配置文件中添加<one-to-many>配置 實現單向多對一: 在many方的實體中添加one方的引用 在many方的配置文件中添加<many-to-one>配置 常用屬性: cascade:設置級聯關系 inverse:設置由哪一方維護關聯關系查看全部
-
org.hibernate.HibernateException: get is not valid without active transaction 沒有開啟事務查看全部
-
//把查詢班級中包含學生的方法完善了一下,班號可以在程序中輸入 import javax.swing.JOptionPane; public static void findStudentsByGrade(){ Session session=HibernateUtil.getSession(); //顯示一個要求輸入班級號碼的對話框 String inputValue = JOptionPane.showInputDialog("請問你想查幾班的同學?輸入班號1、2、3:"); // 原來代碼 Grade grade=(Grade) session.get(Grade.class, 3); Grade grade=(Grade) session.get(Grade.class, Integer.parseInt(inputValue)); //原來代碼 System.out.println(grade.getGname()+","+grade.getGdesc()); //下面是用了一個對話框來顯示數據 JOptionPane.showMessageDialog(null, grade.getGname(), "加油學習,你要查詢的是:"+grade.getGdesc(), JOptionPane.YES_NO_CANCEL_OPTION); Set<Student> students=grade.getStudents(); // for(Student stu:students){ // System.out.println("姓名:"+stu.getSname()+",性別:"+stu.getSex()); // } String showtheText=""; for(Student stu:students){ showtheText = showtheText +"姓名:"+stu.getSname()+",性別:"+stu.getSex()+"\n"; } JOptionPane.showMessageDialog(null, showtheText, "查詢的結果是",JOptionPane.PLAIN_MESSAGE); }查看全部
-
1.配置hbm.xml文件: <hibernate-mapping> <class name="com.imooc.entity.Grade" table="grade"> <id name="gid" column="gid" type="java.lang.Integer"> 主鍵 <generator class="increment"><generator> 這里指選擇最大的序號開始增加 </id> <property name="gname" type="java.lang.String"> 實體類中的屬性 <column name="gname" length="20" not-null="true"></column>對應的數據表中的字段 <property> <!-- 配置一對多的集合屬性 table 指對應表的名稱--> <set name="students" table="student"> <key column="gid"></key> <one-to-many class="com.bjp0425.entity.Student"></one> </set> 2.在cfg.xml中指定文件的路徑:<!--配置映射--> <mapping resourse="com/imooc/entity/Grade.hbm.xml"/> 路徑要用斜杠 注:set集合元素不能重復,且無序查看全部
-
create table grade ( gid int primary key, gname varchar(20) not null, gdesc varchar(50) ); create table student ( sid int primary key, sname varchar(20) not null, sex char(2), gid int ); alter table student add constraint fk_student_gid foreign key (gid) references grade(gid);查看全部
-
hibernate4.3的初始化 用于會話的獲取和關閉: public class HibernateUtil{ private static SessionFactory sessionFactory; private static Session session;} static{ //穿件configuration對象,讀取hibernate.cfg.configure(); StandardsrciceRegistryBuilder ssrb=new SrandarsServlceRedistryBuilser() .applySettings(config.getProperties()); SrandardSerciceRegistry ssr=ssrb.build(); sessionFactory=config.buildSessionFactory(ssr); } //創建會話工廠sessionFactory獲取 public static SessionFactory getSessionFactory(){ return sessionFactory; } //獲取session public static Session getSession(){ session=sessionFactory.openSession(); return session; } //關閉session public static void closeSession(Sessionsession){ if(session!=null){ session.close(); } }查看全部
舉報
0/150
提交
取消