我是 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]
我有四个名为 - 顾客 - 元素 - 命令 - 订单ID
它们都以一对多和多对一的关系连接。
这是一些代码
@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>
最佳答案
您应该添加:
<mapping class="Your_class_name_here"/>
像这样:
<mapping class="Customer"/>
在你的 persistence.xml 中。
另外,你应该看看at this nice tutorials .
希望这有帮助!
关于java - 使用 JPA 和 Hibernate 创建表(连接表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16895376/