Hibernate:使用带有EJB3注释的java类生成hbm.xmls

时间:2013-05-07 09:10:35

标签: hibernate hbm

我需要使用带有EJB3注释的java类创建Hibernate XML Mapping文件。 我在 Eclipse Java EE IDE for Web Developers Juno Service Release 2 下使用了Hibernate Tools。用于创建Hibernate XML Mapping文件,如this post中所述 但我有同样的问题(在那篇文章中提到):EJB3注释将被忽略。

如果我有以下POJO:

package org.hibernate.tutorial.domain;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "EVENTS")
public class Event {
    @Id
    @GenericGenerator(name = "eGen", strategy = "uuid")
    @GeneratedValue(generator = "eGen")
    @Column(name = "EVENT_ID")
    private String id;

    @Column(name = "EVENT_TITLE", length = 2730)
    private String title;


    @Column(name = "EVENT_LONG_TITLE", length = 130)

    private String longTitle;

    public String getLongTitle() {
        return longTitle;
    }

    public void setLongTitle(String longTitle) {
        this.longTitle = longTitle;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "EVENT_DATE")
    private Date date;

    public Event() {
    }

    public String getId() {
        return id;
    }

    private void setId(String id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

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

然后将生成以下映射器

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 07.05.2013 11:08:58 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="org.hibernate.tutorial.domain.Event" table="EVENT">
        <id name="id" type="java.lang.String">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="TITLE" />
        </property>
        <property name="longTitle" type="java.lang.String">
            <column name="LONGTITLE" />
        </property>
        <property name="date" type="java.util.Date">
            <column name="DATE" />
        </property>
    </class>
</hibernate-mapping>

非常感谢您的回答!

1 个答案:

答案 0 :(得分:0)

id生成器不同,第一个使用“uuid”而hbm表示“已分配”,列名和表名也不同。