users@glassfish.java.net

Re: Constraints not working

From: <glassfish_at_javadesktop.org>
Date: Wed, 31 Oct 2007 07:46:12 PST

I just tried this and it seemed to work with the table creation:

CREATE TABLE foo (myid BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL, FIRSTNAME VARCHAR(255) UNIQUE NOT NULL, PRIMARY KEY (myid))



package entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Entity class NewEntity
 *
 * @author Owner
 */
@Entity
@Table(name="foo")
public class NewEntity implements Serializable {

    @Id
    @Column(name="myid", precision=15)
    //_at_GeneratedValue(strategy = GenerationType.AUTO)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(nullable = false, unique = true)
    private String firstName;

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

    public String getFirstName() {
        return firstName;
    }

    /** Creates a new instance of NewEntity */
    public NewEntity() {
    }

    /**
     * Gets the id of this NewEntity.
     * @return the id
     */
    public Long getId() {
        return this.id;
    }

    /**
     * Sets the id of this NewEntity to the specified value.
     * @param id the new id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Returns a hash code value for the object. This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    /**
     * Determines whether another object is equal to this NewEntity. The result is
     * <code>true</code> if and only if the argument is not null and is a NewEntity object that
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof NewEntity)) {
            return false;
        }
        NewEntity other = (NewEntity)object;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
        return true;
    }

    /**
     * Returns a string representation of the object. This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "entity.NewEntity[id=" + id + "]";
    }
    
}
[Message sent by forum member 'lancea' (lancea)]

http://forums.java.net/jive/thread.jspa?messageID=243153