javing
javing

Reputation: 12433

Object relational mapping with JPA Tools: Generate Tables from Entities feature

I have a problem when i use the JPA feature "Generate Tables from Entities" in eclipse. I managed to do all of my ORM mappings for my project, but there is just one that is making me trouble.

The console says this:

Internal Exception: java.sql.SQLSyntaxErrorException: 'OFFER_ID' is not a column in table or VTI 'COMMENT'. Error Code: -1

This is an image of how the tables should look like:

enter image description here

-One offer has many Comments

-One comment belong to one Offer ...

This is how the entities look like: The entity comment:

@Entity
public class Comment {
// Attributes
@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
      ...
@ManyToOne
@JoinColumn(name = "OFFER_ID", nullable = false)
private Offer offer;// One comment must belong to one offer
...Getters and setters

The entity Offer:

@Entity
public class Offer {
   //...Other attributes

    @OneToMany(mappedBy = "offer")
private List<Coupon> coupons;//One offer can have many coupons

@OneToMany(mappedBy = "offer")
private List<Comment> comments; //One offer can have many comments

    ... getters and setters

In the class Offer on purpose i pasted the relationship the class offer has with another class called coupons, as you see it is exactly the same as the one that offer should have with the class Comment.

So what is the problem?

Why one relationship gets mapped and other no?

Why the new created table in the database COMMENT does not have a column called OFFER_ID?

How can i fix it?

Upvotes: 0

Views: 1493

Answers (2)

zawhtut
zawhtut

Reputation: 8561

May be you need <property name="hibernate.hbm2ddl.auto" value="update"/> in persistence.xml

Upvotes: 0

weekens
weekens

Reputation: 8292

I believe, the line

@JoinColumn(name = "OFFER_ID", nullable = false)

causes you the trouble. Because you really don't have column OFFER_ID in Offer table.

To fix that, add an id field to your Offer entity:

@Id(columnName = "OFFER_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

I may be wrong with the syntax...

Upvotes: 1

Related Questions