当我在两个类之间有一对多的关系时,我无法从数据库获得结果。当我为单个表(与任何表无关)编写相同的查询时,它工作正常。
这是我的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)
方法的结果应该是根据方法的参数从数据库中获取的对象。
答案 0 :(得分:0)
在您的require "set"
def compare(check_array, standard
standard.to_set.superset?(check_array.to_set) # return true if check_array is subset of standard
end
实体类中,更改您的Copies
映射。
booktype
也在您的@ManyToOne
@JoinColumn(name = "id", referencedColumnName = "id")
private BookType booktype;
实体类中:
BookType
这将在这两个实体之间映射双向映射。