Hi
I'm quite new to Hibernate and getting desperate about it. I'm trying to achieve a very simple thing.
I'm using Hibernate 4.2.8 with persistence.xml file. My db is mysql 5
I have a one-to-many relationship between two classes: expertise and expertisecategory. I want both classes to have a unique auto incremented id field. I also want the mapping between both tables/classes to be bi-directional. I've included below how I declared my two classes.
The issue is that the id fields don't get the 'auto_increment' label in MySQL. instead, a table called "hibernate_sequence" is created.While this may be working when accessing the db through the app, I can't insert new rows from another tool (MySQL CLI for instance, because this seems to break the sequence and I get 'no duplicate' errors). I tried adding the @column() annotation to force the auto_increment label, but in this case, the expertise table is not created because of an error saying that there can be only one auto column (here's the create table command that gets generated: create table expertise (expertise_id BIGINT NOT NULL AUTO_INCREMENT not null auto_increment, code varchar(255), name varchar(255), category_id BIGINT NOT NULL AUTO_INCREMENT not null, primary key (expertise_id))"
Could someone help achieving this very simple thing? I need: 2 tables/classes that related to each other in a bi-directional way be able to update the database either through the application (and hibernate) or directly in the CLI without breaking the sequence
Many thanks for your help
Jean-Noël
@Entity @Table(name = "expertise") public class Expertise { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long expertise_id; private String code; private String name; @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinColumn(name = "category_id", nullable = false) private ExpertiseCategory category;
@Entity @Table(name = "expertisecategory") public class ExpertiseCategory { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long category_id; private String type; private String code; private String name; private int idx; @OneToMany(mappedBy = "category") @OrderBy("name") private List<Expertise> expertises = new ArrayList<Expertise>();
|