-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Please with auto_increment fields
PostPosted: Mon Dec 23, 2013 5:28 am 
Newbie

Joined: Mon Dec 23, 2013 5:17 am
Posts: 1
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>();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.