1、 问题
有entity class 没有数据库表。 比如WcLoginInfo, 用的是hibernate 。。。需要反向工程自动生成数据库里面的表结构。
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
/**
* WcLoginInfo entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name="logininfotable"
,catalog="hcapi"
)
public class WcLoginInfo implements java.io.Serializable {
// Fields
private String infoId;
private String userLoginName;
// Property accessors
@Id
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@GeneratedValue(generator = "system-uuid")
@Column(name="infoId", unique=true, nullable=false, length=45)
public String getInfoId() {
return this.infoId;
}
public void setInfoId(String infoId) {
this.infoId = infoId;
}
@Column(name="userLoginName", nullable=false, length=45)
public String getUserLoginName() {
return this.userLoginName;
}
public void setUserLoginName(String userLoginName) {
this.userLoginName = userLoginName;
}
}
2.解决办法
persistence.xml 里 加入 红色的 那行 。。。。
<?xmlversion="1.0"encoding="UTF-8"?>
<persistenceversion="2.0"xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unitname="wechat"transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.wc.bean.WcLoginInfo</class>
<properties>
<propertyname="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>
<propertyname="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/hcapi?useUnicode=true&characterEncoding=UTF-8"/>
<propertyname="hibernate.connection.username"value="root"/>
<propertyname="hibernate.connection.password"
value=""/>
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
注意:
value="create" to build a new database on each run;
value="create-drop" means the same as "create" but also drops tables whenHibernate closes;
value="update" to modify an existing database;
value="validate" makes no changes to the database
create:表示启动的时候先drop,再create create-drop: 也表示创建,只不过再系统关闭前执行一下drop update: 这个操作启动的时候会去检查schema是否一致,如果不一致会做scheme更新 validate: 启动时验证现有schema与你配置的hibernate是否一致,如果不一致就抛出异常,并不做更新
所以当项目第一次启动数据库表建立成功以后,为了保持数据不丢失,下次启动项目前需要删除 或 注掉 <property name="hibernate.hbm2ddl.auto" value="create" />