简体   繁体   中英

Java JPA bidirectional ManyToOne mapping not working

I am trying to setup a new JPA bidirectional mapping between and existing table and a new one. It doesn't seem to work. The entity manager doesn't initialize during run-time.

The other existing tables seem to work with no issues. Also, the app starts fine, if i remove this new table and its associated object from persistence xml.

The app also works fine, when the new table is added by itself, without the foreign key join ie. OneToMany or ManyToOne mapping.

Can someone please help me identify what is missing?

Exception noted:

ERROR SomeService:104 - General Error: Something wrong happened in JVM
java.lang.NoClassDefFoundError: Could not initialize class com.java.path.persistence.DefaultEntityManager

CREATE TABLE IF NOT EXISTS new_entity (
    id                      BIGSERIAL PRIMARY KEY,
    existing_tbl_id         BIGINT NOT NULL
);

Existing Entity:

CREATE TABLE IF NOT EXISTS existing_entity (
    id                      BIGSERIAL PRIMARY KEY
);

ExistingEntity.java

@Entity
@Table(name = "existing_entity")
public class ExistingEntity {
   @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

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

    @OneToMany(mappedBy = "existingEntity", 
            cascade = CascadeType.ALL, 
            fetch = FetchType.EAGER)
    private List<NewEntity> newEntities = new ArrayList<>();


    public List<NewEntity> getNewEntities() {
        return newEntities;
    }

    public void setNewEntities(List<NewEntity> newEntities) {
        newEntities.forEach(newEntity -> 
        newEntity.setExistingEntity(this)
        );
        this.newEntities = newEntities;
    }

    public void addNewEntities(NewEntity newEntity) {
        if (newEntity != null) {
            newEntity.setExistingEntity(this);
            newEntities.add(newEntity);
        }
    }
}

NewEntity.java

@Entity
@Table(name = "new_entity")
public class NewEntity {
   @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "existing_tbl_id")
    private ExistingEntity existingEntity;

    public ExistingEntity getExistingEntity() {
        return existingEntity;
    }

    public void setExistingEntity(ExistingEntity existingEntity) {
        this.existingEntity = existingEntity;
    }

}

I have also attempted the below, but it doesn't work:

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "existing_tbl_id", referencedColumnName = "id")
    private ExistingEntity existingEntity;

Solution: Switching the Fetch type to Lazy did it as below:

    @OneToMany(mappedBy = "existingEntity", 
    cascade = CascadeType.ALL, 
    fetch = FetchType.LAZY)
    private List<NewEntity> newEntities = new ArrayList<>();


I am not sure how the fetch style could cause this difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM