加入相关jar包后先配置初始的struts、spring 文件 (因为hibernate 是交给了spring处理 所以只需配置web.xml、struts.xml、spring得applicationContext.xml)
现在问题出来了:如何使用jpa来配置相关的数据关系。(关于jpa的资料在我的资源里)。
我先写了一个继承类:HibernateEntity
import java.io.Serializable; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.MappedSuperclass; import javax.persistence.Transient; import org.hibernate.annotations.GenericGenerator; @MappedSuperclass @SuppressWarnings("unchecked") public abstract class HibernateEntity implements Serializable { protected T id; /** * Id */ @Id @Column(name="BE_ID") @GeneratedValue(generator = "IDGEN") @GenericGenerator(name = "IDGEN", strategy = "native") public T getId() { return id; } public void setId(T id) { this.id = id; } /** * 是否为新建对象(未持久化) */ @Transient public boolean isNew() { return (this.id == null); } @Override public int hashCode() { int hashCode = 0; hashCode = 29 * hashCode + (id == null ? 0 : id.hashCode()); return hashCode; } @Override public boolean equals(Object object) { if (this == object) { return true; } if (!(object.getClass().equals(this.getClass()))) { return false; } final HibernateEntity that = (HibernateEntity ) object; if (this.getId() == null || that.getId() == null || !this.getId().equals(that.getId())) { return false; } return true; } @Override public String toString() { return (this.getClass().getName() + " - " + id); } }
之后写一个需要生成的实体bean :TestBean
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "NCVA_TestBean") public class TestBean extends HibernateEntity { private String name; @Column(name="test_name") public String getName() { return name; } public void setName(String name) { this.name = name; } }
在spring的配置文件中的sessionFactory下配置:
配置Hibernater的Session工厂 自动生成数据库表 设置BEAN的类路径 com.loyin.Bean.TestBean
请问我的java写法或者配置有没什么问题 但是总算不能自动生成数据库表。
希望大家能给予我好的方案,谢谢