0

Probably a silly question but I don't get it. So I've got a class like:

@Entity
@Table(name="colour"
    ,catalog="car_store"
)
public class Colour  implements java.io.Serializable {


     private Byte id;
     private String name;
     private Set<Car> cars = new HashSet<Car>(0);

    public Colour() {
    }


    public Colour(String name) {
        this.name = name;
    }
    public Colour(String name, Set<Car> cars) {
       this.name = name;
       this.cars = cars;
    }

     @Id @GeneratedValue(strategy=IDENTITY)


    @Column(name="id", unique=true, nullable=false)
    public Byte getId() {
        return this.id;
    }

    public void setId(Byte id) {
        this.id = id;
    }


    @Column(name="name", nullable=false)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

@OneToMany(fetch=FetchType.LAZY, mappedBy="colour")
    public Set<Car> getCars() {
        return this.cars;
    }

    public void setCars(Set<Car> cars) {
        this.cars = cars;
    }
}

So if I'm trying to insert like:

Session session = HibernateUtil.getSessionFactory().openSession();
Colour newColour = new Colour();

newColour.setName("Deep Sea Blue");
session.save(newColour);

session.getTransaction().commit();

it should not automaticly generate new id value during this? Because it doesn't.

What am I doing wrong?

Thanks.

user3623571
  • 47
  • 1
  • 7
  • http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier – Stefan May 21 '14 at 14:45
  • I've see that and I suppose my mapping is right. I get and set all class's properties and etc, and if I even put annotations in front where class's properties declared, like in the example, It doesn't change anything at all, in other ways I’ve got everything just like in the example. – user3623571 May 21 '14 at 14:57

2 Answers2

0

Try to remove

@Column(name="id", unique=true, nullable=false)

from

public Byte getId()

It looks redundant. And leave only:

@Id @GeneratedValue(strategy=IDENTITY)
public Byte getId() {
        return this.id;
    }
Faresis
  • 103
  • 5
  • I agree, have removed that. However, new id unfortunately is still not generated. – user3623571 May 21 '14 at 15:12
  • Please also check [this](http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Common_Problems_4). – Faresis May 21 '14 at 15:24
  • Mysql database itself is configured right. When I do INSERT INTO colour (name) VALUES("Deep Sea Blue"); it work just fine. So if it's not my inserting code nor maping not database itself. What else could it be according to that article, Hibernate? I don't get it. Could you help me out? – user3623571 May 22 '14 at 09:00
  • Have you tried to change strategy to AUTO? And also I see that in Hibernate documentation Long type is used whereas you are using Byte. I assume there can be problems with this. Try to change it to Long. – Faresis May 22 '14 at 10:33
  • Oh my! Thanks a lot! I've changed type to Long at the mapping and there it is! Changing strategy to auto doesn't effect anyway. Please post the answer. Thanks again. – user3623571 May 22 '14 at 12:54
0

In the documentation provided type of the property is Long. In the example provided by you Byte is used. I assume there can be problems with this. Try to change it to Long.

Faresis
  • 103
  • 5