0

我是 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]

我有四个实体,称为 - Customer - Item - 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>
4

2 回答 2

0

您应该添加:

<mapping class="Your_class_name_here"/>

像这样:

<mapping class="Customer"/>

在您的 persistence.xml 中。

另外,您应该看看这个不错的教程

希望这可以帮助 !

于 2013-06-03T11:52:21.220 回答
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>
于 2013-06-03T12:09:16.613 回答