Hi !
I'm quite new to EJB3, Hibernate annotations and Hibernate entityManager and I'm having a problem making hibernate generate foreign keys when mapping relations.
I've create 2 relations, many-to-one and many-to-many, and neither cause a foreign key to be created !
My entity looks like that :
Code:
@Entity
@Table(name = "test_user")
public class TestUserH implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false)
private String login;
@Column(nullable = false)
private String password;
@ManyToMany(targetEntity=TestRoleH.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(name="test_user_role",
joinColumns={@JoinColumn(name="user_id")},
inverseJoinColumns={@JoinColumn(name="role_id")}
)
private Set roles;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@ForeignKey(name="fk_role")
// @JoinColumn(name="role_id")
private TestRoleH role;
// Nothing interessant from here...
}
And TestRoleH :
Code:
@SuppressWarnings("serial")
@Entity
@Table(name = "test_role")
public class TestRoleH implements Serializable {
public static final String DEFAULT_ROLE_GROUP = "Roles";
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false)
private String name;
@Column(nullable = false, name="group_name")
private String group;
@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE},
mappedBy="roles",
targetEntity=TestUserH.class
)
private Set users;
// Nothing interessant from here...
}
hbm2ddl is configured as 'update'.
I'm using Hibernate 3.2.2 with PostgreSQL 8.2 under JBoss 4.0.5
Thanks for your help !
--
CĂ©line[/code]