0

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!

Stephen Carroll
  • 67
  • 1
  • 10
  • it simply means JPA or hibernate is not able to find the package that contains your entities. – vaibhavsahu Aug 30 '21 at 15:31
  • where are your datasource beans? – vaibhavsahu Aug 30 '21 at 15:32
  • @vaibhavsahu Updated with my Configuration class. I thought EntityScan and ComponentScan on the Application and Entity and Component on the corresponding classes was enough to help spring to find them. Is there something else I need? – Stephen Carroll Aug 30 '21 at 15:50
  • 1
    try this @EntityScan(basePackages = {"package name of your entities"}) – vaibhavsahu Aug 30 '21 at 16:12
  • I added that and got a different error about insertable/updateable, changed that, and now I'm getting the unmapped class exception again for the mapping of the ratings field within Movie. `Use of @OneToMany or @ManyToMany targeting an unmapped class: com.stephenalexander.projects.moviecollection.movie.Movie.ratings[com.stephenalexander.projects.moviecollection.rating.Rating]`Updated my code above. – Stephen Carroll Aug 30 '21 at 18:06
  • check this url to fix your current errors -> https://stackoverflow.com/questions/4956855/hibernate-problem-use-of-onetomany-or-manytomany-targeting-an-unmapped-clas – vaibhavsahu Aug 30 '21 at 18:40
  • @vaibhavsahu I've gone through that thread a few times. Everything seems to be in line except my not having the configuration files, but I think that aspect of things should be covered with my scan annotations. It actually booted up a few times, and I was working on things, and then I added a JsonIgnore because I had a Stack Overflow, and now I am getting the unmapped class error again. I removed the annotation, reverting it back to where it was when it worked, and the error is still there. I don't understand what the root of it is and why it is so inconsistent with throwing that error. – Stephen Carroll Aug 30 '21 at 21:12

0 Answers0