【发布时间】:2019-07-03 08:35:59
【问题描述】:
当我在两个类之间有一对多关系时,我无法从数据库中获取结果。当我为单个表(与任何表无关)编写相同的查询时,它工作正常。
这是我的 BookType 类:
@Entity
@Table(name="booktype")
public class BookType {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="typeBook")
private String type;
@Column(name="price")
private double price;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "id")
private Set<Copies> copies;
public BookType() {
super();
}
public BookType(String type, double price) {
super();
this.type = type;
this.cena = price;
}
geters and seters...
这是我的 Cpoies 课程:
@Entity
@Table(name="copy")
public class Copies {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="quantity")
private int quantity;
@Column(name="tempQuantity")
private int tempQuantity;
@Column(name="actualyCopyQuantity")
private int actualyCopyQuantity;
@ManyToOne
private BookType booktype;
public Copies(int quantity, BookType type) {
this.quantity = quantity;
this.tempQuantity = 0;
this.actualyCopyQuantity = this.quantity-this.tempQuantity;
this.booktype = type;
}
public Copies() {
}
geters and seters...
查询在这个方法中:
public BookType findTypeByTypeName(String type) {
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(BookType.class)
.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
TypedQuery<BookType> query = session.createQuery("from
BookType BT where BT.type ='"+ type+"'");
BookType singleType = query.getSingleResult();
session.getTransaction().commit();
session.close();
return singleType;
}
最后是例外:
Exception in thread "main" org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: aca.model.BookType.copies[aca.model.Copies]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1330)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:868)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:793)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:53)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1684)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1652)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:473)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:84)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at aca.service.BookService.findTypeByTypeName(BookService.java:170)
at test.Main.main(Main.java:33)
方法的结果应该是根据方法的参数从数据库中获取的对象。
【问题讨论】:
标签: hibernate hibernate-onetomany hibernate-query