将 hibernate3 与现有表一起使用

发布于 2024-11-06 06:12:30 字数 3912 浏览 0 评论 0原文

如何配置 Hibernate 以便将 bean 映射到某些现有表?

PS:每个表应该对应一个实体。

编辑:这是我如何将 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/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">

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

How can I configure Hibernate so it maps beans to some existing tables?

P.S: Each table should correspond with an entity.

Edit: Here is how I used Hibernate with autogenerated tables (taken from a web application). I'm still not clear on how to make the transition to some explicit mapping.

Example Entity 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/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">

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

☆獨立☆ 2024-11-13 06:12:30

(“填充”是什么意思?将 beans 映射到表?然后:)

旧方法:为每个表/实体创建 Hibernate *.hbm.xml。没什么特别的,过去就做过。

新方法:创建 bean,用 Hibernate 注释标记它们。

新的新方式:使用 JPA 注释进行可移植映射,并使用 Hibernate 作为 JPA 实现。

(What do you mean "fills in"? Map beans to tables? Then:)

Old way: create Hibernate *.hbm.xml per table/entity. Nothing extraordinary, did it in past.

New way: create beans, mark them up with the Hibernate annotations.

New new way: use JPA annotations for portable mapping and Hibernate as JPA implementation.

烟沫凡尘 2024-11-13 06:12:30

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 选项-对多等。关系。

Hibernate annotations allow you to specify the table and column names. I'm adding the batch size and cache option as an extra, but that's not required for your question.

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

   @Id
   protected String theID;

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

Have a look at all the options in @Table @Column @Index and the @JoinColumn option for the one-to-many et al. relationships.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文