user7035864
user7035864

Reputation:

Nested exception is javax.persistence.EntityNotFoundException

I'm using spring boot and and spring data in my project and i have two classes:

class Mission implements Serializable { private static final long 
serialVersionUID = 1L;
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private Long              id;
private String            departure;
private String            arrival;
private Boolean           isFreeWayEnabled;
@OneToMany( mappedBy = "mission" )
private List<Station>     stations;
// getters and setters
}

and the second class is :

@Entity
public class Station implements Serializable {

private static final long serialVersionUID = 1L;

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

@ManyToOne( fetch = FetchType.LAZY )
@JsonBackReference
private Mission           mission;
//getters and setters
 }

Methode which add Mission:

public Mission addMision( Mission mission ) {
// TODO Auto-generated method stub
// Mission mission = getMissionById( mission.getId() );
for ( Station station : mission.getStations() ) {
    station.setMission( mission );
    stationRepository.save( station );
}
return missionRepository.save( mission );
}

when i tried to add a new Mission it gives this error :

"Unable to find com.carpooling.entity.Station with id 2; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.carpooling.entity.Station with id 2"

heres the JSON object Sent:

{"departure":"fff","arrival":"ffff","isFreeWayEnabled":false,"stations":[{"id":1},{"id":2}]}

Upvotes: 2

Views: 16899

Answers (2)

manuelvigarcia
manuelvigarcia

Reputation: 2104

If you do things right...

In my case the error was due to trying to use @ElementCollection annotation for a map of non-@Embeddable objects (actually, a `Map<Integer, @Entity>.

My solution went through mapping objects in a flatter way to make them likeable by JPA. The detail was getting rid of the parent entity that acted as aggregator and saving by hand the different collections that it was holding (three repositories instead of one, three save() invocations instead of one, one less table).

Upvotes: 0

surya
surya

Reputation: 2749

You may need to change it little bit.

If you use @ManyToOne, the referenced entity must exist. Otherwise specify that field as a long which you have already have and retrieve the referenced entity by means of a separate query.

It throws an exception (javax.persistence.EntityNotFoundException) instead of returning null if it can't find the requested entity.

Use @NotFound annotation to resolve this exception if you are lazy loading and not handling this exception manually. I have given a syntax ..

@ManyToOne(
    fetch = FetchType.LAZY)
@NotFound(
    action = NotFoundAction.IGNORE)
@JoinColumn(
    name = COLUMN,
    referencedColumnName = COLUMN,
    insertable = false,
    updatable = false)
private Mission mission;

Upvotes: 5

Related Questions