I have read a ton of different questions and answers on these errors and can't see what is wrong with my annotations. I had a functioning setup but recently swapped to having Spring update my tables in Postgres, and I'm running into walls with annotations and column joining again. I apologize for the 100th question on here regarding the same errors, but I have tried so many configs with no luck. There was a point where I'd seemingly randomly get the error about targeting an unmapped class upon startup. It would sometimes work fine and other times give me the error, which has really confused me.
I am trying to have Movies, Raters, and Ratings. Movies have a One to Many with Ratings, which have a Many to One with Raters. Here are my classes:
@Entity
@Table(name = "movie")
public class Movie implements Serializable {
@Id
@Column(name = "movie_id")
private String id;
private String title;
private Integer year;
@Column(name = "runningtime")
private Integer runningTime;
@OneToMany(mappedBy="movie", cascade=CascadeType.ALL)
@JsonIgnore
private List<Rating> ratings;
@Entity(name = "Rating")
@Table(name = "rating")
public class Rating {
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
@Column(name = "rating_id")
private Long id;
@Column(name = "rating")
private Double value;
@Column(name = "created_at", insertable = false)
private LocalDateTime time;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL,
targetEntity = Movie.class)
@JoinColumn(name = "movie_id")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private Movie movie;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL,
targetEntity = Rater.class)
@JoinColumn(name = "rater_id", insertable = false, updatable = false)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonManagedReference
private Rater rater;
@Entity(name = "Rater")
@Table(name = "rater")
public class Rater {
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
@Column(name = "rater_id")
private Long id;
@Column(name = "created_at", insertable = false)
private LocalDateTime time;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = Rating.class, mappedBy = "rater")
@JsonBackReference
private List<Rating> ratings;
@EnableAutoConfiguration
@EnableJpaRepositories
@SpringBootApplication
@EntityScan(basePackages = "com.stephenalexander.projects.moviecollection.*")
@ComponentScan(basePackages = { "com.stephenalexander.projects.moviecollection.*" })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Configuration
public class Config {
@Autowired
private MovieRepository movieRepository;
@Autowired
private RatingRepository ratingRepository;
@Autowired
private RaterRepository raterRepository;
@Autowired
private MovieService movieService;
@Autowired
private RatingService ratingService;
@Autowired
private RaterService raterService;
@Bean
public ParserFactory parserFactory() {
return new ParserFactory();
}
}
Some notes: I have seen things about having a persistence.xml file. I do not have one and looked into making one, but as I understand it, if my annotations are correct and I have my Application class scanning correctly, I shouldn't need that file for my simple app.
I seem to get different errors depending on the class that's on my current view in IntelliJ when I hit bootRun. Is there an ordering to things based on your focus at the time you hit it?
At this point, I have had so many bandaids put on this small app just to get it to spin up, I assume there are a bunch of unnecessary annotations. If any seem batty, please let me know.
I have checked my imports and nothing is coming from a foreign package.
Thanks for the help!
EDIT: It is currently working. The change that got it to boot up was removing the further directory and asterisk from the basePackages
parameter given to @EntityScan
and @ComponentScan
. They now read as follows:
@EntityScan(basePackages = { "com.stephenalexander.projects.moviecollection" })
@ComponentScan(basePackages = { "com.stephenalexander.projects.moviecollection" })
I am not checking this as answered for a few hours to make sure it doesn't give me some unexpected error next time I try to boot up, as this has happened before. Thanks for pointing me back to the scan config, @vaibhavsahu.
EDIT 2: I left this yesterday with it unable to boot, as it was throwing the unmapped class error again. I came back to it this morning and all seems to be well. I can't replicate it, but I expect it to be back at some point!