开发者

Different identifier of entity by persist in Hibernate

开发者 https://www.devze.com 2023-02-22 13:10 出处:网络
When I create a new entity with no ID and store it in a database, an entity receives an identifier other than the identifier in the database. ID is generated via sequence in oracle. Any advice how to

When I create a new entity with no ID and store it in a database, an entity receives an identifier other than the identifier in the database. ID is generated via sequence in oracle. Any advice how to solve it? Thanks for reply.

Example: I create DiReview review = new DiReview(). I set correctly all fields except id a i want hibernate persist via getSessionFactory().getCurrentSession().persist() review to database and i want hibernate set correct id. Suppose, that the last ID generated by oracle sequence was 25,so i assume, that a new row in table DI_REVIEW will be 26. After persist and commit is in table really stored new row with id 26 but to field id in review is set another number. In my case for example 2000 !! This is normal?

This problem is in my case related not only to DiReview but all my entities. When i am loading entities from database, there are loaded with corect id.

Edit - I tried to implement this example using oracle and sequence and at least now I know that it's not normal behavior ;-)

@Entity
@Table(name = "DI_REVIEW", uniqueConstraints = @UniqueConstraint(columnNames = {
        "OBJECT_ID", "USER_ID" }))
public class DiReview{

    private Long id;
    private DiU开发者_如何学编程ser user;
    private DiObject object;
    private String text;
    private Date createDate;

    private Set<DiRating> ratings = new HashSet<DiRating>(0);
    private Set<DiReviewContext> reviewContexts = new HashSet<DiReviewContext>(
            0);

    private Collection<DiReviewContext> reviewContextsList = new ArrayList<DiReviewContext>();
    private Set<DiComment> comments = new HashSet<DiComment>(0);

    public DiReview() {
    }

    public DiReview(Long id, DiUser user, DiObject object, String text,
            Date createDate) {
        this.id = id;
        this.user = user;
        this.object = object;
        this.text = text;
        this.createDate = createDate;
    }

    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 18, scale = 0)
    @SequenceGenerator(name = "di_review_seq", sequenceName = "DI_REVIEW_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "di_review_seq")
    public Long getId() {
        return this.id;
    }

    getters and setters ..

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((createDate == null) ? 0 : createDate.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((object == null) ? 0 : object.hashCode());
        result = prime * result + ((text == null) ? 0 : text.hashCode());
        result = prime * result + ((user == null) ? 0 : user.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DiReview other = (DiReview) obj;
        if (createDate == null) {
            if (other.createDate != null)
                return false;
        } else if (!createDate.equals(other.createDate))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (object == null) {
            if (other.object != null)
                return false;
        } else if (!object.equals(other.object))
            return false;
        if (text == null) {
            if (other.text != null)
                return false;
        } else if (!text.equals(other.text))
            return false;
        if (user == null) {
            if (other.user != null)
                return false;
        } else if (!user.equals(other.user))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "DiReview [id=" + id + ", user=" + user + ", object=" + object
                + ", text=" + text + ", createDate=" + createDate + "]";
    }

}

    public T makePersistent(T entity) {
    try {

        getSessionFactory().getCurrentSession().persist(entity);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return entity;
}


I'm not sure if this addresses your question, but when using sequence generators, Hibernate, by default, allocates a block of 50 ID values at one time, which may mean that it can't grab the sequence value you might expect. You can alter this by changing the annotation for @SequenceGenerator as follows:

@SequenceGenerator(name = "di_review_seq", sequenceName = "DI_REVIEW_SEQ", 
        allocationSize = 1)
0

精彩评论

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