使用带有Hibernate的JPA创建表(连接表)

时间:2013-06-03 11:12:16

标签: java mysql database jpa

我是JPA世界的新手,在这里我遇到了这个问题。

我尝试的是使用

从带注释的实体中导出模式
    new SchemaExport(config).create(true, true);

它给了我这个错误

Exception in thread "main" org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.hidvd.entities.Customer.orders[com.hidvd.entities.Order]

我有四个实体叫 - 客户 - 项目 - 订单 - OrderId

它们都与一对多和多对一的关系联系在一起。

以下是一些代码

@Entity
public class Customer {
    @Id
    @Column(name = "cust_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int custid;
    @Column(length = 50, nullable = false, unique = true)
    private String name;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
    private List<Order> orders = new ArrayList<Order>();
}

@Entity(name = "item")
public class Item implements Serializable{
    public Item() {
        super();
    }

    @Id
    @Basic(optional = false)
    @Column(name = "item_id", unique = true, nullable = false)
    private int itemid;
    private String title;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "item")
    private List<Order> orders = new ArrayList<Order>();



@Entity(name = "order")
@AssociationOverrides({
        @AssociationOverride(name = "customer",
                joinColumns = @JoinColumn(name = "cust_id")),
        @AssociationOverride(name = "item",
                joinColumns = @JoinColumn(name = "item_id")) })
public class Order implements Serializable {

    private OrderId pk = new OrderId();
    private Calendar orderedDate;


@Embeddable
public class OrderId implements Serializable{
    @ManyToOne
    private Customer customer;
    @ManyToOne
    private Item item;

任何建议将不胜感激。在此先感谢:)

  • 这是persistence.xml文件
<persistence-unit name="hidvddb">
    <jta-data-source>java:/MySQLDS</jta-data-source>
        <properties>
            <property name="showSql" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

2 个答案:

答案 0 :(得分:0)

你应该添加:

<mapping class="Your_class_name_here"/>

像这样:

<mapping class="Customer"/>

在persistence.xml中。

另外,你应该看看at this nice tutorials

希望这有帮助!

答案 1 :(得分:0)

以下是persistence.xml文件的示例:

<persistence-unit name="hidvddb">
     <jta-data-source>java:/MySQLDS</jta-data-source>
          <class>com.hidvd.entities.Customer</class>
          <class>com.hidvd.entities.Item </class>
          <class>com.hidvd.entities.Order </class>
          <class>com.hidvd.entities.Order </class>        

          <properties>
             <property name="showSql" value="true"/>
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
          </properties>
</persistence-unit>

或者如果您不想在persistence.xml文件中声明所有实体

 <persistence-unit name="hidvddb">
         <jta-data-source>java:/MySQLDS</jta-data-source>
          <exclude-unlisted-classes>false</exclude-unlisted-classes>         

              <properties>
                 <property name="showSql" value="true"/>
                 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
              </properties>
    </persistence-unit>
</persistence>