I have two entities, a State and a Location. A State can have many locations. This mapping should be pretty straightforward.
@Entity
@Table(name="Locations")
public class Location {
public Location() {};
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToOne
@JoinColumn(name="StateCode")
private State state;
}
and the State class:
@Entity
@Table(name="States")
public class State {
private static final Logger log = LoggerFactory.getLogger(State.class);
public State() {
locations = new ArrayList<Location>();
};
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="location")
private Collection<Location> locations;
@Id
@Column(name="StateCode", nullable=false, length=6)
private String stateCode;
public Collection<Location> getLocations() {
return locations;
}
public void setLocations( Collection<Location> locations ) {
this.locations = locations;
}
public String getStateCode() {
return stateCode;
}
public void setStateCode(String code) {
this.stateCode = code;
}
and yet I get this error: AnnotationException mappedBy reference an unknown target entity property: net.rallaesystems.vibracheck.model.Location.location in net.rallaesystems.vibracheck.model.State.locations
I have followed the advice of changing the mapping to the 'state' field. With this code, though
@Repository
public interface StateRepository extends CrudRepository<State, String>{
List<State> findAll();
}
being called in a simple controller, the serialization of States goes into an infinite loop. Maybe there is a problem with the serialization library? I think it is Jackson.