日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hibernate 多对一外键注解

發布時間:2025/3/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hibernate 多对一外键注解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

ClassRoom.java package com.com;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;

@Entity public class ClassRoom { @Id @GeneratedValue(generator="cid") @GenericGenerator(name="cid",strategy="assigned") @Column(length=5) private String cid; private String cname;

public ClassRoom(){}public ClassRoom(String cid, String cname) {super();this.cid = cid;this.cname = cname; }public String getCid() {return cid; }public void setCid(String cid) {this.cid = cid; }public String getCname() {return cname; }public void setCname(String cname) {this.cname = cname; }

}

Mao.java package com.com;

import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne;

@Entity public class Mao {

private int sid; private String phone; private String post; private ClassRoom classroom; @ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER) @JoinColumn(name="cid",referencedColumnName="CID")public ClassRoom getClassroom() {return classroom; }public void setClassroom(ClassRoom classroom) {this.classroom = classroom; }public Mao(){}public Mao(int sid, String phone, String post) {super();this.sid = sid;this.phone = phone;this.post = post; } @Id public int getSid() {return sid; }public void setSid(int sid) {this.sid = sid; }public String getPhone() {return phone; }public void setPhone(String phone) {this.phone = phone; }public String getPost() {return post; }public void setPost(String post) {this.post = post; }

} Test.java package com.mao;

import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Test { @org.junit.Test public void test(){ Configuration config=new Configuration().configure(); ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); SchemaExport export=new SchemaExport(config); export.create(true,true); sessionFactory.close();

}

@org.junit.Test public void Add(){ Configuration config=new Configuration().configure(); ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); Session session=sessionFactory.openSession(); Transaction tx=session.beginTransaction();

tx.commit();session.close();sessionFactory.close();

} @org.junit.Test public void addd(){ Configuration config=new Configuration().configure(); ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry); SchemaExport export=new SchemaExport(config); export.create(true,true); sessionFactory.close();

} } 出現錯誤,多敲幾次代碼

轉載于:https://my.oschina.net/u/2511906/blog/842389

新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的hibernate 多对一外键注解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。