org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类

时间:2017-01-06 08:59:00

标签: java hibernate jpa one-to-many

我正在尝试使用onetomany映射产品规范,其中我从描述中复制了类似的工作代码。我提供了所有代码。

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: sm-unit] Unable to build EntityManagerFactory
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
        at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
        ... 38 more
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.salesmanager.core.business.catalog.product.model.Product.specifications[com.salesmanager.core.business.catalog.product.model.specification.ProductSpecification]
        at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1204)

Product.java的条目如下:

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product")
    private Set<ProductSpecification> specifications = new HashSet<ProductSpecification>();

ProductSpecification.java有以下代码..

@Entity
@Table(name = "PRODUCT_SPECIFICATION", schema=SchemaConstant.BEAWLP_JAMBIRA_SCHEMA)
public class ProductSpecification extends SalesManagerEntity<Long, ProductSpecification> {
    private static final long serialVersionUID = -7991123525661321865L;

    @ManyToOne(targetEntity = Product.class)
    @JoinColumn(name = "PRODUCT_ID", nullable = false)
    private Product product;

@Id
@Column(name = "SPECIFICATION_ID", unique = true, nullable = false)
@TableGenerator(name = "TABLE_GEN", table = "SM_SEQUENCER", pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", pkColumnValue = "SPECIFICATION_SEQ_NEXT_VAL")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
private Long id;

@Column(name="TITLE", length=100)
private String title;

public ProductSpecification() {
}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

4 个答案:

答案 0 :(得分:1)

从错误消息中可以知道实体未正确映射。

  • 确保您已导入javax.persistence.entity而不是org.hibernate.annotations.entity
  • 另外,请确保实体列在配置文件中(persistence.xml,hibernate.cfg.xml)。

答案 1 :(得分:0)

我认为你错过了targetEntity,尝试使用下面的行可能对你有帮助。

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product", targetEntity = ProductSpecification.class)
private Set<ProductSpecification> specifications = new HashSet<ProductSpecification>();

答案 2 :(得分:0)

我注意到使用Set和HashSet时会发生异常。您可以改为使用List和ArrayList

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, mappedBy = "product")
private List<ProductSpecification> specifications = new ArrayList<ProductSpecification>();

答案 3 :(得分:0)

对我来说,是通过将类添加到

来解决的
favorite.component.html

<div class="dir-alp-con-right-1">
      <div class="row">
        <div class="row span-none" *ngIf="!isSpinner">
          <div class="col-md-4" *ngFor="let x of RentData | paginate: { itemsPerPage: 9, currentPage: p }">
            <a (click)="sendRentdetails(x)">
              <!-- <a [routerLink]="['/saleconcreteplantdetails']" >   -->
              <div class="list-mig-like-com com-mar-bot-30">
                <div class="list-mig-lc-img"> <img src="{{x.image1}}" alt="Image"> </div>
              </div>
            </a>
                <div class="list-mig-lc-con">
                    <div class="primeimage">
                        <button class="btn btn-success">Rs. {{x.price}}</button>
                        <button class="favouri" (click)="isFavoritee(x.ad_Id)"><i class="fa"
                          [ngClass]="{'fa-heart' : isFavorite , 'fa-heart-o' : !isFavorite }"></i></button>
                    </div>
                  <div class="list-rat-ch list-room-rati">
                    <i class="fa fa-map-marker" aria-hidden="true"></i> {{x.location}} </div>
                  <h5>{{x.name}}</h5>
                  <h6>Other Machines > For Rent Other Machines</h6>
                </div>
          </div>
        </div>
      </div>
    </div>