Hibernate使用SchemaExport和AnnotationConfiguration生成表结构

step 1 :定义entity

@Entity
@Table(name="T_ADM_USER")
public class User extends GenericEntity implements Serializable {
	
	@OneToOne(cascade = CascadeType.PERSIST)
	@JoinColumn(name="grade_id")
	public Grade getGrade() {
		return grade;
	}
	public void setGrade(Grade grade) {
		this.grade = grade;
	}
	@Column(name="user_name", insertable = true, updatable = true, nullable = true)
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

} 


step 2:Main方法类

 

 

public class TableUtil {

	public static void main(String[] args) {
		try{
			
			AnnotationConfiguration cfg = new AnnotationConfiguration();
			cfg.addAnnotatedClass(com.mygogo.grade.user.entity.User.class);
                        ...
			SchemaExport se = new SchemaExport(cfg);
			se.setDelimiter(";");
			se.drop(true, true);
			se.create(true, true);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
}


 

 

你可能感兴趣的:(java)