2011-03-10 7 views
16

L'entità generica, super classe:Hibernate: Come ignorare un attributo da mappato super classe

@MappedSuperclass 
public abstract class GenericEntity { 
    private Integer id; 
    public Integer getId() {return id;} 
    public void setId(Integer id) {this.id = id;} 
} 

Il POJO:

@Entity 
@Table(name = "POJO_ONE") 
@SequenceGenerator(name = "HB_SEQ_POJO_ONE", sequenceName = "SEQ_POJO_ONE", allocationSize = 1) 
public class PojoOne extends GenericEntity { 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HB_SEQ_POJO_ONE") 
    @Column(name = "ID") 
    @AttributeOverride(name = "id", column = @Column(name = "ID")) 
    private Integer id; 

    @Override 
    public Integer getId() {return id;} 
} 

cerco di usare thoses annotazioni: @AttributeOverride, @Id, ... ma non funziona. Mi potete aiutare? Voglio sovrascrivere l'attributo "id" per specificare un altro nome di colonna e una sequenza per pojo/tabella. Qual è il modo migliore per farlo?

+0

http://opensource.atlassian.com/projects/hibernate/browse/HHH-4380 – axtavt

risposta

28

Prova questa, invece

@MappedSuperclass 
public abstract class GenericEntity { 
    private Integer id; 
    ... 

    public Integer getId() {return id;} 
    public void setId(Integer id) {this.id = id;} 
} 


@Entity 
@Table(name = "POJO_ONE") 
@SequenceGenerator(name = "HB_SEQ_POJO_ONE", sequenceName = "SEQ_POJO_ONE", allocationSize = 1) 
@AttributeOverride(name = "id", column = @Column(name = "ID")) 
public class PojoOne extends GenericEntity { 
    // we should not define id here again 
    ... 

    @Override 
    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HB_SEQ_POJO_ONE") 
    public Integer getId() {return id;} 
} 
+0

Con questa soluzione, quando provo a salvare, ho questa eccezione: 'org.hibernate.id.IdentifierGenerationException: gli ID per questa classe devono essere assegnati manualmente prima di chiamare save()' – BasicCoder

+0

@ user616564: Quindi basta prendere '@GeneratedValue 'annotazione al' MappedSuperClass', ma le sottoclassi non sarebbero in grado di dare la propria implementazione. Se per te va bene. –

+0

E come posso specificare una sequenza per tabella/entità per l'id? – BasicCoder

4

Perché non si annota lo id di GenericEntity con @Id? Non è inoltre necessario ridefinire il numero id ma inserire lo @AttributeOverride(name = "id", column = @Column(name = "ID")) nella classe anziché in un campo.

Edit:

Stiamo usando questo nella nostra classe di base (package.OurTableGenerator è la nostra propria implementazione):

@GeneratedValue (generator = "ourTableGenerator", strategy = GenerationType.TABLE) 
@GenericGenerator (name = "ourTableGenerator", strategy = "package.OurTableGenerator", 
    parameters = { @Parameter (name = OurTableGenerator.TABLE_PARAM, value = "t_sequence"), 
       @Parameter (name = OurTableGenerator.SEGMENT_COLUMN_PARAM, value = "c_entity"), 
       @Parameter (name = OurTableGenerator.VALUE_COLUMN_PARAM, value = "c_nextHi"), 
       @Parameter (name = OurTableGenerator.INCREMENT_SIZE_COLUMN_PARAM, value = "c_blocksize") }) 
@Id 
@Column(name = "c_uid") 
private Long uid; 

Questo Diamo Precisiamo una dimensione del blocco differenc e la sequenza per ogni entità/tabella.

Per il proprio generatore di tabelle è possibile creare sottoclasse org.hibernate.id.TableGenerator.

+0

E come posso specificare una sequenza da tavolo/entità per l'id? – BasicCoder

+0

Ho aggiunto il codice che stiamo usando per la sequenza per tabella. – Thomas

+0

Sarà fantastico se puoi pubblicare il codice sorgente della classe OurTableGenerator –

Problemi correlati