开发者

Setting subclass primary key as auto_increment using JPA 2 annotations

I'm trying to generate a database schema for my project using hbm2ddl. I'm using JPA 2 annotations to specify how the schema should look like. Right now I'm having some issues with inherited id's.

I have an abstract super class, let's call it AbstractSuperClass, which looks like this:

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractSuperClass {
    ...
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
    }
    ...
}

I've set the id to be an auto generated value which translates to SQL's auto_increment constraint. However w开发者_Go百科hen I look at the generated script, I don't see the id columns of the subclass tables to have the auto_increment in them.

Anyone has an idea how I could get that? Of course I could manually specify it but as much as possible I want it automated.

Thanks.


Not supported, as mentioned in the Reference Documentation in the section about inheritance mapping:

2.2.4.1. Table per class

This strategy has many drawbacks (esp. with polymorphic queries and associations) explained in the JPA spec, the Hibernate reference documentation, Hibernate in Action, and many other places. Hibernate work around most of them implementing this strategy using SQL UNION queries. It is commonly used for the top level of an inheritance hierarchy:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Flight implements Serializable { ... }            

This strategy supports one-to-many associations provided that they are bidirectional. This strategy does not support the IDENTITY generator strategy: the id has to be shared across several tables. Consequently, when using this strategy, you should not use AUTO nor IDENTITY.


Note that GenerationType.AUTO means that it's up to the persistence provider to select a generation strategy, have you tried a more specific value?


For the record, the guy is asking how to auto_increment he's id columns using JPA 2 annotations.

Dude, just declare strategy=GenerationType.IDENTITY on your @GeneratedValue annotation. e.g. :

@Id  
@GeneratedValue(strategy=GenerationType.IDENTITY)  

protected long anyIdhere;

Hope this will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜