就是我之前数据库已经有很多表了,我现在新写了个实体类,和..hbm.xml文件,现在想把这个表映射到数据库,就是在数据库创建出这个表,怎么整?不能数据库表都删了然后都重新创建,是创建一个新表不影响数据库中的原有表

解决方案 »

  1.   

    楼主我建议你用annotation  这样当你项目一启动的时候,如果没有实体对应的且,他会自动帮你创建一个的
    如果有了,他不会帮你建了..
      

  2.   

    而且现在写实体,很少人用.hbm.xml文件了,多麻烦.一般都是用annotation的方式,省很多事.
      

  3.   

    实体类: 如下
    package cn.wang.usermanager.entities;import java.io.Serializable;public class User implements Serializable { private static final long serialVersionUID = 710652653888362545L; private Integer id;

    private String name;

    private String password;

    private String sex;

    private String address;

    private String email; public User() {}

    public Integer getId() {
    return id;
    } public void setId(Integer id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    } public String getSex() {
    return sex;
    } public void setSex(String sex) {
    this.sex = sex;
    } public String getAddress() {
    return address;
    } public void setAddress(String address) {
    this.address = address;
    } public String getEmail() {
    return email;
    } public void setEmail(String email) {
    this.email = email;
    }
    }
    对应的映射文件: 
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.wang.usermanager.entities">
    <class name="User" table="t_user">
    <id name="id">
    <generator class="native"/>
    </id>
    <property name="name" not-null="true" length="15" />
    <property name="password" not-null="true" length="15"/>
    <property name="sex" />
    <property name="address" />
    <property name="email"/>
    </class>
    </hibernate-mapping><!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
    <session-factory>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/ssh</property>
    <property name="connection.username">root</property>
    <property name="connection.password">LIUWANG521</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="connection.autocommit">true</property>

    <mapping resource="cn/wang/usermanager/entities/User.hbm.xml"/>

    <!--
    <class-cache
    class="org.hibernate.test.legacy.Simple"
    region="Simple"
    usage="read-write"/>
    -->
    </session-factory>
    </hibernate-configuration>
    spring配置文件
    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置sessionFactory -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

    <!--  配置事务管理器    -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
            <property name="sessionFactory" ref="sessionFactory" />  
        </bean>
      
        <!--配置事务的传播特性   -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">  
            <tx:attributes>  
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />  
                <tx:method name="update*" propagation="REQUIRED" />  
                <tx:method name="*" read-only="true" />  
            </tx:attributes>  
        </tx:advice> 
        
        <!--那些类的哪些方法参与事务   -->
        <aop:config proxy-target-class="true">  
         <aop:pointcut expression="execution(* cn.wang.usermanager.managers.managerimpl*.*(..))" id="allManagerMethod"/>
            <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />  
        </aop:config>

    <bean id="userManager" class="cn.wang.usermanager.managers.impls.UserMangerImpl">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean name="/user" class="cn.wang.usermanager.actions.UserAction">
    <property name="userManager" ref="userManager"/>
    </bean>
    </beans>
      

  4.   

    你使用Hibernate提供的数据库表生成工具手动生成不就可以了
      

  5.   

    用系统的那个hibernate生成数据库表就行的啦,如果它和其他表有关联的话,全都选中就OK了,不过现在进了公司才知道,那种方法很少人用了。都用annotation。连数据库表都不用设计了。不过有点麻烦就是,写起来还是比写sql难多了