简体   繁体   中英

Hibernate GeneratedValue using GenericGenerator is ignored

This field "auftragsnummer" always stays null, even though it is annotated with @GeneratedValue:

@Entity
public class Auftrag implements Serializable
{
   @Id
   @GeneratedValue
   private int id;

   @Pattern(regexp = AUFTRAGSNUMMER_REGEXP, message = "{validator.auftragsnummer}")
   @Length(min = 20, max = 20)
   @GenericGenerator(name = "sequence_auftragsnummer", strategy = "de.software.AuftragsnummerGenerator")
   @GeneratedValue(generator = "sequence_auftragsnummer")
   @Column(unique = true, nullable = false)
   private String auftragsnummer;
}

I store it using getHibernateTemplate().persist(t) . The referenced generator class implements org.hibernate.id.IdentifierGenerator . I have no idea why it is ignored.

Environment:

  • Hibernate 5.0.1
  • Spring 4.2.1
  • Java 7
  • DB2 10

JPA only mandates support for @GeneratedValue on @Id fields.

If your order number doesn't depend on the entity's primary key, then the easiest solution is to use @PrePersist eg

@PrePersist
public void onCreate() {
    auftragsnummer = ...;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM