Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not set field value by reflection

I have a problem with a many to many relationship. The tables are: ingredient and nutional value. I created a table of relations between the 2 entities, in which there are the external keys of ingredient and nutrional value (which form the compound key) and some attributes.

Class JoinedNutrionalValueIngredient:

import lombok.Data;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@Entity @Data
public class JoinedNutrionalValueIngredient implements Serializable {

    @EmbeddedId
    private NutrionalValueIngredientId id;

    @ManyToOne(fetch= FetchType.LAZY)
    @MapsId("ingredient_id")
    private Ingredient ingredient;

    @ManyToOne(fetch=FetchType.LAZY)
    @MapsId("nutrional_value_id")
    private NutrionalValue nutrionalValue;

    @NotNull
    String matrixUnit;

    @NotNull
    int value;

    @NotNull
    String valueType;
}

Class NutrionalValueIngredientId:

import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;

@Embeddable
@Getter
@Setter
public class NutrionalValueIngredientId implements Serializable{

    @Column(name = "ingredient_id")
    private Long ingredient_id;

    @Column(name = "nutrional_value_id")
    private Long nutrional_value_id;

    public NutrionalValueIngredientId() {
        
    }   
   
    public NutrionalValueIngredientId(Long ingredient, Long nutrionalValue){
        this.ingredient_id=ingredient;
        this.nutrional_value_id=nutrionalValue;
    }
    
    public boolean equals(Object o) {
        if (this == o) return true;
    
        if (o == null || getClass() != o.getClass())
            return false;
    
        NutrionalValueIngredientId that = (NutrionalValueIngredientId) o;
        return Objects.equals(ingredient_id, that.ingredient_id) &&
                    Objects.equals(nutrional_value_id, that.nutrional_value_id);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(ingredient_id, nutrional_value_id);
    }
}

When I try to insert a new field inside the relationship table I get this error:

{
  "timestamp": 1542653896247,
  "status": 500,
  "error": "Internal Server Error",
  "message": "Could not set field value [1] value by reflection : [class com.whateat.reciper.model.NutrionalValueIngredientId.ingredient_id] setter of com.whateat.reciper.model.NutrionalValueIngredientId.ingredient_id; nested exception is org.hibernate.PropertyAccessException: Could not set field value [1] value by reflection : [class com.whateat.reciper.model.NutrionalValueIngredientId.ingredient_id] setter of com.whateat.reciper.model.NutrionalValueIngredientId.ingredient_id",
  "path": "/v1/joinedNutrionalValueIngredients"
}

Edit: I added constructor and annotation @Getter and @Setter, but I have the same error.

class NutritionalValue:

@Data
@Entity
public class NutrionalValue implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String name;

    @NotNull
    private String unit;

    @NotNull
    private String source;

    @ManyToOne
    @NotNull
    @JoinColumn(name = "category_id")
    private NutrionalValueCategory category;

    @OneToMany(mappedBy = "nutrionalValue")
    private Set<JoinedNutrionalValueIngredient> joined = new HashSet<JoinedNutrionalValueIngredient>();

}

edit: after Debopam's answer, this error came out.

{
  "timestamp": 1542657216244,
  "status": 500,
  "error": "Internal Server Error",
  "message": "null id generated for:class com.whateat.reciper.model.JoinedNutrionalValueIngredient; nested exception is org.hibernate.id.IdentifierGenerationException: null id generated for:class com.whateat.reciper.model.JoinedNutrionalValueIngredient",
  "path": "/v1/joinedNutrionalValueIngredients"
}
like image 266
Francesco Avatar asked Nov 19 '18 19:11

Francesco


People also ask

How do you set a field in a reflection?

The set() method of java. lang. reflect. Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter.


1 Answers

 @Entity @Data
 public class JoinedNutrionalValueIngredient implements Serializable {
    
        @EmbeddedId
        private NutrionalValueIngredientId id = new NutrionalValueIngredientId();
    
    // ... rest of class  
 }

In the JoinedNutrionalValueIngredient class the compound id NutrionalValueIngredientId shall be instansiated.

like image 69
Otti Avatar answered Sep 20 '22 07:09

Otti