我遇到了一个问题,因为没有使用hibernate在MySQL数据库上生成表。我正在使用注释创建表。
实体类:
@Entity
public class Countrydetail {
private Integer countryid ;
private String countryname;
private String description;
/**
* @return the countryid
*/
@Id
@Column(name ="country-id" , nullable= false )
@GeneratedValue(strategy=GenerationType.AUTO )
public Integer getCountryid() {
return countryid;
}
/**
* @param countryid the countryid to set
*/
public void setCountryid(Integer countryid) {
this.countryid = countryid;
}
/**
* @return the countryname
*/
@Column( name = "country_name" ,nullable = false)
public String getCountryname() {
return countryname;
}
/**
* @param countryname the countryname to set
*/
public void setCountryname(String countryname) {
this.countryname = countryname;
}
/**
* @return the description
*/
@Column( name = "Description" ,nullable = false)
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
}
Hibernate配置管理器类:
public class RouternHibernateConfigManager {
private static SessionFactory factory = null;
static{
try{
Configuration config = new AnnotationConfiguration().configure("/hibernate.cfg.xml")
.addAnnotatedClass(Countrydetail.class);
factory = config.buildSessionFactory();
} catch (HibernateException ex) {
System.err.println("Initial sessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static Countrydetail getCountryDetail(int id)
{
Session session = factory.openSession();
Countrydetail cd = new Countrydetail();
cd.setCountryname(cd.getCountryname());
cd.setDescription(cd.getDescription());
session.close();
return cd;
}
public static void saveCountryDetail(Countrydetail cd2){
Countrydetail cd = new Countrydetail();
cd.setCountryname(cd2.getCountryname());
cd.setDescription(cd2.getDescription());
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
try{
session.save(cd);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
session.close();
}
}
}
//...
}
Hibernate配置文件(摘录):
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/Routerninfo</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!--<property name="hibernate.default_schema">CUSTOMERDATAS</property>-->
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect
</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property>-->
<!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
<mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/>-->
<mapping class="com.routerndata.state.bean.Countrydetail"/>
CountryManager:
public class CountryManager {
public static void handelCountryDetail(HttpServletRequest request, HttpServletResponse response){
String countryname = request.getParameter(CountryConstant.COUNTRYNAME);
String description = request.getParameter(CountryConstant.DESCRIPTION);
Countrydetail cd = new Countrydetail();
cd.setCountryname(countryname);
cd.setDescription(description);
RouternHibernateConfigManager.saveCountryDetail(cd);
}
}
答案 0 :(得分:0)
尝试在hibernate.cfg.xml中取消注释:
40%
这将强制删除现有表并创建架构,销毁以前的数据。
此外,您应该使用camelCase作为您的bean名称和变量。阅读起来非常困难,这让下一个人认为你的XML中可能存在配置错误。