Difference between @GeneratedValue and @GenericGenerator

后端 未结 3 1003
不知归路
不知归路 2020-12-04 13:24

Sometimes I find them together, sometimes alone... other times they seem to do the same.

What\'s the difference?

Here are three examples. What do they do of

3条回答
  •  悲&欢浪女
    2020-12-04 13:36

    To extend the @kevin-bowersox's answer.
    Relationships between the Hibernate primary key generation strategies and specific generator respectively, as specified in org.hibernate.id.IdentifierGeneratorFactory

    static {
        GENERATORS.put("uuid", UUIDHexGenerator.class);     // "deprecated" for new use
        GENERATORS.put("hilo", TableHiLoGenerator.class);   // removed in Hibernate 5
        GENERATORS.put("assigned", Assigned.class);
        GENERATORS.put("identity", IdentityGenerator.class);
        GENERATORS.put("select", SelectGenerator.class);
        GENERATORS.put("sequence", SequenceGenerator.class);
        GENERATORS.put("seqhilo", SequenceHiLoGenerator.class);
        GENERATORS.put("increment", IncrementGenerator.class);
        GENERATORS.put("foreign", ForeignGenerator.class);
        GENERATORS.put("guid", GUIDGenerator.class);
        GENERATORS.put("uuid.hex", UUIDHexGenerator.class); // uuid.hex is deprecated
        GENERATORS.put("sequence-identity", SequenceIdentityGenerator.class);
    }
    

    In Hibernate 4.3 I've found org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory class with 3 more strategies:

        register("uuid2", UUIDGenerator.class);
        register("enhanced-sequence", SequenceStyleGenerator.class);
        register("enhanced-table", TableGenerator.class);
    

    The above fifteen strategies, plus native, are sixteen generation strategies supported in Hibernate by default.

    Example with native:

    @GeneratedValue(generator = "nativeGenerator")
    @GenericGenerator(name = "nativeGenerator", strategy = "native")
    

提交回复
热议问题