当前位置:编程学习 > JAVA >>

hibernate怎么通过实体类创建数据库表

我想通过Java的实体类创建数据库表,我该怎么做啊!搞了好长时间没弄出来,求大神帮忙啊! --------------------编程问答-------------------- 这个网上很多 。搜搜看看。 --------------------编程问答--------------------


import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class UserInfo {                 

private int id;                      
private String username;             
private String userpwd;              
private String userphone;           
private String others;              

private Set<HireInfo> hireinfos = new HashSet<HireInfo>(); 
private Set<Magdebrug> magdebrugs = new HashSet<Magdebrug>(); 

@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getUserphone() {
return userphone;
}
public void setUserphone(String userphone) {
this.userphone = userphone;
}
public String getOthers() {
return others;
}
public void setOthers(String others) {
this.others = others;
}

@OneToMany(mappedBy="userinfo")
public Set<HireInfo> getHireinfos() {
return hireinfos;
}
public void setHireinfos(Set<HireInfo> hireinfos) {
this.hireinfos = hireinfos;
}

@OneToMany(mappedBy="userinfo")
public Set<Magdebrug> getMagdebrugs() {
return magdebrugs;
}
public void setMagdebrugs(Set<Magdebrug> magdebrugs) {
this.magdebrugs = magdebrugs;
}
}



public static void main(String[] args) {
Session session=HibernateSessionFactory.getSession();

}

运行一下就可以了 --------------------编程问答-------------------- 其实这个不值得推荐,应为表设计也是比较复杂的,尤其是大一些的系统。 --------------------编程问答--------------------
引用 2 楼 yyl1007931988 的回复:


import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class UserInfo {                 

private int id;                      
private String username;             
private String userpwd;              
private String userphone;           
private String others;              

private Set<HireInfo> hireinfos = new HashSet<HireInfo>(); 
private Set<Magdebrug> magdebrugs = new HashSet<Magdebrug>(); 

@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getUserphone() {
return userphone;
}
public void setUserphone(String userphone) {
this.userphone = userphone;
}
public String getOthers() {
return others;
}
public void setOthers(String others) {
this.others = others;
}

@OneToMany(mappedBy="userinfo")
public Set<HireInfo> getHireinfos() {
return hireinfos;
}
public void setHireinfos(Set<HireInfo> hireinfos) {
this.hireinfos = hireinfos;
}

@OneToMany(mappedBy="userinfo")
public Set<Magdebrug> getMagdebrugs() {
return magdebrugs;
}
public void setMagdebrugs(Set<Magdebrug> magdebrugs) {
this.magdebrugs = magdebrugs;
}
}



public static void main(String[] args) {
Session session=HibernateSessionFactory.getSession();

}

运行一下就可以了

配置hibernate还得加上这个
<prop key="hibernate.hbm2ddl.auto">update</prop> 
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,