Java – hibernate generators are not inserted with uniqueidentifier

I'm trying to map entities using hibernate annotations so that IDs are automatically generated when records are created and saved (through cascading). Using my current settings (or some other settings I've tried), I received the following error:

...org.hibernate.exception.ConstraintViolationException: 
Could not insert: [com.gorkwobbler.shadowrun.karma.domain.Attributescore]
    ...java.sql.sqlException: 
Caused by: java.sql.sqlException: Cannot insert the value NULL into column 'id',table 'KARMA_DEV.dbo.Character'; column does not allow nulls. INSERT fails.

I can see the following insert statement issued:

Hibernate: insert into character (version,alias,firstName,lastName) values (?,?,?)

Obviously, this is wrong. There is no "Id" parameter

My current table mode is very simple:

Character(
 id uniqueidentifier,--primary key
 alias varchar(max),firstName varchar(max),lastName varchar(max),version int --for hibernate
)

I am using SQL Server 2008 R2, Express Edition

My comments separate the mapped superclass domainentity from the concrete class karmacharacter:

@MappedSuperclass
public abstract class DomainEntity implements Serializable /* Needed for HOM retainUnsaved */ {
    private static final long serialVersionUID = 1L;
    private String id;
    private Integer version;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Generated(value=GenerationTime.INSERT)
    //@GeneratedValue(generator="hibernate-uuid.hex")
    //@GenericGenerator(name="hibernate-uuid.hex",strategy="org.hibernate.id.UUIDHexGenerator",parameters=@Parameter(name="separator",value="-"))
    @AccessType(value="field")
    public String getId() {
        return id;
    }

    @Version
    @AccessType(value="field")
    public Integer getVersion() {
        return version;
    }
}

@SuppressWarnings("serial")
@Entity
@Table(name="character")
public class KarmaCharacter extends DomainEntity {
    private String alias;
    private String lastName;
    private String firstName;

    private SortedSet<Attributescore> attributescores;

    public KarmaCharacter() {
        //default constructor
    }

    @Column
    @AccessType(value="field")
    public String getAlias() {
        return alias;
    }

    @Column
    @AccessType(value="field")
    public String getFirstName() {
        return firstName;
    }

    @Column
    @AccessType(value="field")
    public String getLastName() {
        return lastName;
    }

//...omitted some transient code and a collection property for brevity

    public void setAlias(String alias) {
        this.alias = alias;
    } 

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

I would appreciate it if someone could tell me the correct way to generate uniqueidentifier type IDS using hibernate in SQL server and save them correctly

Solution

In fact, when using the uniqueidentifier SQL Server type, hibernate must use newid() But your current comment doesn't tell it to do so I think you need a guid generator:

@Id
@GenericGenerator(name = "generator",strategy = "guid",parameters = {})
@GeneratedValue(generator = "generator")
public String getId() {
    return id;
}

Some supplementary notes:

>The guid column type is actually used to save the guid generated by Microsoft algorithm. You cannot use hibernate UUID algorithm. > You don't need to generate comments on ID, just delete them. > I also want to know why you "messed up" smart accesstypes. I'll delete them. > I don't actually use guids (see this article), but this is another story

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>