Masoom Raza
Masoom Raza

Reputation: 179

Automatic Column name generation for JPA table mapping

I am trying to map entity tables with @ManyToOne and @OneToMany. The mapping column is in the child table named "internal_plan_id". As per the requirement I can not change the names. Below are the two entity tables: PARENT TABLE

import java.sql.Timestamp;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

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

@Entity
@Getter
@Setter
//@Data
@NoArgsConstructor
@Table(name = "financial_plan_details", schema = "financialplanadmin")
public class FinancialPlanDao {

  // This internalId is the primary key of the table.
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "internal_plan_id")
  private int internalId;

  // This stores the plan status into the database table.
  @Column(name = "plan_status")
  @Size(max = 10)
  private String planStatus;

  @Column(name = "presentation_file_key")
  @Size(max = 500)
  private String presentationFileKey;

  @Column(name = "create_timestamp")
  @NotNull
  private Timestamp createdTimestamp;

  @OneToMany(mappedBy = "financialPlan")
  private List<FinancialSubPlan> subPlans;
}

CHILD TABLE :

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "financial_plan_subplan", schema = "financialplanadmin")
@JsonInclude(Include.NON_NULL)
public class FinancialSubPlan {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "subplan_id")
  private int subPlanId;
  
  @Column(name = "external_subplan_id")
  private String externalSubplanId;

  @Column(name = "is_chosen")
  private Boolean subPlanIsChosen;
    
  @ManyToOne
  @JoinColumn(name = "internal_plan_id")
  private FinancialPlanDao financialPlan; 
}

I am getting the error as : ERROR: column "internal_plan_id_internal_plan_id" of relation "financial_plan_subplan" does not exist.

The existing column name for mapping in financial_subplan is "internal_plan_id". The above name "internal_plan_id_internal_plan_id" is automatically generated. So is there any way to reduce this to only "internal_plan_id".

Upvotes: 1

Views: 1582

Answers (1)

Masoom Raza
Masoom Raza

Reputation: 179

The problem was with setting values of the mapped classes. The first thing after forming up the parent class, is to set the parent class into the child class, that is subPlans.set(financialPlan). Then after that we have to set the child class into the parent class, that is financialPlan.set(List of subPlan). I missed the setting up of parent into child.

You can also refer to this JPA / Hibernate One to Many Mapping Example with Spring Boot

In this you can see that after creation of Post object, the Comment object sets the Post object and after that the Post object sets the comment object, before saving it to the database.

Upvotes: 1

Related Questions