Home > Blockchain >  Use of @OneToMany or @ManyToMany targeting an unmapped class SpringBoot
Use of @OneToMany or @ManyToMany targeting an unmapped class SpringBoot

Time:09-05

I am currently learning Spring Boot development with Postgresql. Everything was going fine up until today when I tried to add one more OneToMany relation in current entity which resulted in Hibernate exception:

Use of @OneToMany or @ManyToMany targeting an unmapped class: com.github.hryniuklukas.Basic_WMS.model.Document.palletList[com.github.hryniuklukas.Basic_WMS.model.Pallet] 

Unfortunately current answers on SO give me no hint.

Child class:

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.time.LocalDate;

@NoArgsConstructor
@MappedSuperclass
@Entity
@Table(name = "pallet")
@Getter
@Setter
public class Pallet {
    private @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id;
    private String palletCode;
    private LocalDate date;
    private boolean isInWarehouse;
    public Pallet(String palletCode, LocalDate date){
        this.palletCode=palletCode;
        this.date = date;
        this.isInWarehouse = true;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    private PalletSpace palletSpace;
    @ManyToOne(fetch = FetchType.LAZY)
    private Document outboundDocument;
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Pallet )) return false;
        return id != null && id.equals(((Pallet) o).getId());
    }
    public void setPalletStatusAsSent(){
        this.isInWarehouse = false;
    }
    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}

Parent 1:

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;


@NoArgsConstructor
@Getter
@Setter
@Entity(name = "PalletSpace")
@Table(name = "pallet_space")
public class PalletSpace {
    private @Id @GeneratedValue (strategy = GenerationType.IDENTITY) Long id;
    private String spaceCode;

    @OneToMany(
            mappedBy = "palletSpace",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<Pallet> palletList = new ArrayList<>();
    public PalletSpace(String spaceCode){
        this.spaceCode = spaceCode;
    }
    public void addPallet(Pallet pallet){
        palletList.add(pallet);
        pallet.setPalletSpace(this);
    }
    public void removePallet(Pallet pallet){
        palletList.remove(pallet);
        pallet.setPalletSpace(null);
    }
}

Parent 2:

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor
@Getter
@Setter
@Slf4j
@Entity
@Table(name = "document")
public class Document {
  private @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id;

  @OneToMany(
          mappedBy = "outboundDocument",
          cascade = CascadeType.PERSIST
  )
  private List<Pallet> palletList = new ArrayList<>();


  public void addPalletToDocument(Pallet pallet) {
    palletList.add(pallet);
    pallet.setOutboundDocument(this);
  }

  public List<Pallet> getConnectedPalletList() {
    return this.palletList;
  }

  public void removePalletFromDocument(Pallet pallet) {
    palletList.remove(pallet);
    pallet.setOutboundDocument(null);
  }

  public void executeDocument() {
      palletList.forEach(Pallet::setPalletStatusAsSent);
  }
}

Pallet in relation to palletspace worked fine, adding Document to the mix results in exception.

JPA Buddy seems to catch the relations just as it should, showing reference tree correctly, Spring doesnt start thou.

Application properties:

spring.datasource.url=jdbc:postgresql://localhost:5050/postgres
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

CodePudding user response:

Try removing @MappedSuperclass from Pallet, since it shouldn't be mixed with @Entity

  • Related