【问题标题】:Creating tables using JPA with Hibernate (joined tables)使用 JPA 和 Hibernate 创建表(连接表)
【发布时间】:2013-06-03 11:12:15
【问题描述】:

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

我有四个实体称为 - 顾客 - 物品 - 命令 - 订单编号

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

这里有一些代码

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

【问题讨论】:

  • 您能在此处发布您的 persistence.xml 文件吗?似乎休眠没有拿起你的实体
  • @ufasoli 谢谢我上传了。
  • 您忘记在 persistence.xml 文件中列出您的实体。

标签: java mysql database jpa


【解决方案1】:

你应该添加:

<mapping class="Your_class_name_here"/>

像这样:

<mapping class="Customer"/>

在您的 persistence.xml 中。

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

希望这会有所帮助!

【讨论】:

  • 您还可以将 xml 标签 &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt; 添加到您的 persistence.xml 类中,以指示 hibernate 拾取您的带注释的类,即使它们未在您的 persistence.xml 文件中列出
  • 感谢您的回答。也许我忽略了JPA,这太难了哈哈。干杯
【解决方案2】:

以下是您的 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>

【讨论】:

  • 都试过了。但仍然没有运气。感谢您的可能回答。因为我很着急,所以我可能会继续不使用表生成。再次感谢:)
  • 看看教程,看看它们对你有没有帮助。
  • 您使用的是 Spring 还是 CDI 或任何其他 DI 系统?你如何引导你的 JPA 环境?
  • @ufasoli 我只是跟着教程..theopentutorials.com/examples/java-ee/ejb3/…
  • 快把我逼疯了。哈哈哈
猜你喜欢
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 2011-12-25
  • 2015-06-15
  • 2012-12-19
  • 1970-01-01
相关资源
最近更新 更多