【问题标题】:Hibernate one-to-many relationship query problemHibernate 一对多关系查询问题
【发布时间】: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


    【解决方案1】:

    在您的 Copies 实体类中,更改您的 booktype 映射。

    @ManyToOne
    @JoinColumn(name = "id", referencedColumnName = "id")
    private BookType booktype;
    

    也在你的BookType实体类中:

    @OneToMany(cascade = CascadeType.ALL, mappedBy="booktype")
    private Set<Copies> copies;
    

    这将映射这两个实体之间的双向映射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 2018-11-09
      • 2010-09-20
      • 1970-01-01
      • 2010-10-17
      • 2011-12-20
      相关资源
      最近更新 更多