java - 为什么我的表不是使用注释生成的?

标签 java mysql hibernate

我遇到了一个问题,因为表不是在 MySQL 数据库上使用 hibernate 生成的。我正在使用注释创建表格。

实体类:

@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"/>

国家经理:

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);
 }
}

最佳答案

尝试在您的 hibernate.cfg.xml 中取消注释:

<property name="hbm2ddl.auto">create</property>

这将强制删除现有表并创建模式,破坏以前的数据

此外,您应该为 bean 名称和变量使用驼峰命名法。它真的很难读,而且会让下一个人认为您的 XML 中可能存在配置错误。

关于java - 为什么我的表不是使用注释生成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156882/

相关文章:

java - JavaFX 中的绑定(bind)标签 textProperty

php - 从 MYSQL 数据库填充 PHP 对象

php - MySQL查询以显示当前日期在顶部的记录,其他按降序显示

java - Hibernate - 在运行时更改连接字符串

java - HQL:使用聚合函数进行简单选择时出现问题

java - 监控应用程序框架

java - 类路径中的版本控制信息

Java 注释将字段设置为其名称

java - 从地址簿中检索家庭联系电话

php - 如果该行已存在,如何避免重复并选择一行?