0

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;
  • Does this answer your question? [Configure JPA to let PostgreSQL generate the primary key value](https://stackoverflow.com/questions/11825643/configure-jpa-to-let-postgresql-generate-the-primary-key-value) – K.Nicholas Nov 16 '19 at 00:29
  • K.Nicholas, No, it didn't. – user9957199 Nov 16 '19 at 00:35
  • Your new table has a `BIGSERIAL` column type. I'm thinking that needs special treatment. Further, your question is completely lacking in any details needed to help you. – K.Nicholas Nov 16 '19 at 00:37
  • BIGSERIAL has been working fine in all places. The app starts if i have the table added but without the joining FK column. I apologize for less information. Could you please guide me on how better to add details to it? – user9957199 Nov 16 '19 at 00:40
  • Please add the schema for `existing_entity` and `new_entity` and the error message you get when the app fails. The schema you provided is named `new_table` but neither of your classes refer to a `new_table` name. You should also create a DDL from your entities and compare it to your schema to see what the differences are. – K.Nicholas Nov 16 '19 at 00:43
  • https://stackoverflow.com/questions/36966337/how-to-generate-a-ddl-creation-script-with-a-modern-spring-boot-data-jpa-and-h – K.Nicholas Nov 16 '19 at 00:45
  • sorry, it was a typo. I have added some details above. I am attempting to start the app with hibernate.hbm2ddl.auto=create to compare the schema – user9957199 Nov 16 '19 at 00:48
  • I am having trouble getting hibernate.hbm2ddl.auto=create to create the schema. – user9957199 Nov 16 '19 at 01:00
  • I have no troubles: https://github.com/karlnicholas/jpamin.git – K.Nicholas Nov 16 '19 at 01:36
  • Thanks for this project. I will check my project to understand what is different – user9957199 Nov 16 '19 at 02:16

1 Answers1

0

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.