Question

I want to take three pojos that I have and use Hibernate's ability to generate tables in my database that match them. The classes are Movie, Rater, and Rating. Here is Movie. Rater is practically the same thing as it also has a list of Ratings connected to it.

public class Movie{    
    @Id
    @GeneratedValue(generator = "IdGenerator", strategy = Generation Type.TABLE)
    @TableGenerator(name = "IdGenerator",
    pkColumnValue = "movieId")
    private long id;
    private String name;
    @OneToMany(mappedBy = "id.rater")
    private List<Rating> ratings;
}

Rating is much different. I am using an EmbeddedId. Here is what I have so far.

public class Rating{
    @Embeddable
    public static class RatingId
        {
            @ManyToOne
            @JoinColumn(name = "movieId")
            private Movie movie;
            @ManyToOne
            @JoinColumn(name = "raterId")
            private Rater rater;
        }
    @EmbeddedId
    private RatingId id;
    private String comment;
    private int rating;
    }

So I ask this: What special annotation is required for using this embedded id setup? Also, which is the proper keyword for the hbm2ddl value? It would appear that I want to use the "create" value, but reading more on it says that it will cause Hibernate to empty the pre-existing table. Did I read that right? Thanks.

Was it helpful?

Solution

about the keyword for hbm2ddl, "update": updates the schema, "create": creates the schema, destroying previous data. so, i guess "update" is the keyword you are looking for.?

coming to the annotation that should be used to set up embedded id, it is "@EmbeddedID". it is used when the primary key itself needs to be an embedded object. what do you mean "What special annotation is required for using this embedded id setup?"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top