2011-05-10 17 views
1

如何配置Hibernate,以便將Bean映射到某些現有表?在現有表中使用hibernate3

P.S:每個表都應該與一個實體對應。

編輯:這是我如何使用Hibernate與自動生成的表(從Web應用程序中獲取)。我仍然不清楚如何過渡到一些明確的映射。

實例實體Bean

@Entity 
@Table(name = "t1_category") 
// PK is replaced by Long 
// See generic definition in abstract class 
public class Category extends BaseEntity<Long> { 

    @Column(name="check_rsv") 
    private String check; 

    @Column(unique = true) 
    private String name; 
    private static final long serialVersionUID = 1L; 

    public String getCheck() { 
     return this.check; 
    } 

    public void setCheck(String check) { 
     this.check = check; 
    } 
    public String getName() { 
     return this.name; 
    } 

    public void setName(String title) { 
     this.name = title; 
    } 

} 

的applicationContext.xml /春

<?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"> 

    <context:annotation-config /> 
    <context:component-scan base-package="***" /> 


    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="myPersistenceUnit" /> 
    </bean> 

    <!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) --> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" 
      p:entityManagerFactory-ref="entityManagerFactory"/> 

    <tx:annotation-driven/> <!-- This apparently scans for annotations from the base-package value --> 
</beans> 

的persistence.xml

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"> 
    <persistence-unit name="myPersistenceUnit"> 

    <provider>org.hibernate.ejb.HibernatePersistence</provider> 

     <properties> 
      <property name="hibernate.archive.autodetection" value="class"/> 

      <property name="hibernate.connection.url" 
        value="jdbc:mysql://localhost:3306/***?autoReconnect=true"/> 
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
      <property name="hibernate.connection.password" value="***"/> 
      <property name="hibernate.connection.username" value="***"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> 

      <property name="hibernate.hbm2ddl.auto" value="validate"/> 

      <property name="hibernate.show_sql" value="true"/> 
      <property name="hibernate.format_sql" value="true"/> 
      <property name="hibernate.use_sql_comments" value="true"/> 
     </properties> 
    </persistence-unit> 
</persistence> 
+1

您可以使用Hibernate Tools的_Reverse Engineering_功能完成您的工作。看看這裏 - http://docs.jboss.org/tools/3.2.0.GA/en/hibernatetools/html_single/index.html#reverseengineering – bobah 2011-05-10 07:18:07

回答

1

(你是什麼意思 「在填寫」?地圖豆表那麼:)

舊的方法:爲每個表/實體創建Hibernate * .hbm.xml。沒什麼特別的,過去做過。

新方法:創建bean,用Hibernate註釋標記它們。

新的新方法:使用JPA批註進行便攜式映射,並使用Hibernate作爲JPA實現。

1

Hibernate註釋允許您指定表名和列名。我將批量大小和緩存選項作爲額外的添加,但這不是您的問題所必需的。

@Entity 
@Table(name="BLAH") 
@BatchSize(size=20) 
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL) 
public class Blah{ 

    @Id 
    protected String theID; 

    @Column(name="MEH") 
    protected int meh; 
} 

有在@Table@Column@Index看看所有的選項,併爲一個一對多等人的@JoinColumn選項。關係。

相關問題