Link19
Link19

Reputation: 606

Why would Hibernate not create my tables when @GenerateValue added to Entities?

I have an application with 4 entities backed by postgresql. When I run the application without the @GenerateValue annotation on my id fields the tables are all created fine, however if I add it in and start up I get a massive stack trace which basically is telling me that my relations do not exist.

And I'm all like: "Well duh! You're supposed to be making them exist"

I have the correct generation stuff in my config, which is evident in the fact that the tables are being generated when the annotation is left off.

Here's some changed code from some of my entities:

@Entity
@Table(name = "MyClass",uniqueConstraints=@UniqueConstraint(columnNames={"name","parent"}))
public class MyClass implements Serializable {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String mapName = "";

@ManyToOne
@JoinColumn(name = "parent_id")
private MyParentClass parent;

And it's parent:

@Entity
@Table(name = "PARENT",uniqueConstraints=@UniqueConstraint(columnNames={"name"}))
public class MyParentClass implements Serializable {
static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

String name = "";

@OneToMany(mappedBy = "parent")
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
@LazyCollection(LazyCollectionOption.FALSE)
private Set<MyClass> children = new HashSet<MyClass>();

Am I doing something wrong, or is this a problem with Hibernate?

Upvotes: 0

Views: 302

Answers (2)

Link19
Link19

Reputation: 606

The problem was that I had the wrong hibernate dialect in my config. Was set to MYSQL which worrying worked for a lot of the stuff I was doing, and I wasn't seeing warnings for it. Worth keeping this about in case this does come up for anyone else.

Upvotes: 0

Mikko Maunu
Mikko Maunu

Reputation: 42074

Without stacktrace it is bit hard to say is this only problem, but

uniqueConstraints=@UniqueConstraint(columnNames={"name","parent"})

seems to be wrong. There is no columns name and parent, instead you have columns named mapName and parent_id.

Upvotes: 1

Related Questions