openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexey Romanov <alexey.v.roma...@gmail.com>
Subject Why is NOT NULL generated for @UniqueConstraint?
Date Mon, 18 Jun 2012 13:52:40 GMT
With this class definition:

package ru.focusmedia.odp.server.datastore.jpa.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PostLoad;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import ru.focusmedia.odp.server.datastore.api.objects.OdpObjectRecord;

@SuppressWarnings("serial")
@Entity
@Table(name = "objects", uniqueConstraints = { @UniqueConstraint(columnNames
= {
		"name", "parent_id" }) })
public class OdpObjectEntity implements Serializable, OdpObjectRecord {
	@Id
	@GeneratedValue
	private Long id;

	@Column(nullable = false)
	private String name;

	@ManyToOne
	@JoinColumn(name = "parent_id")
	private OdpObjectEntity parent;

	@ManyToOne
	@JoinColumn(name = "object_class_id", nullable = false)
	private OdpObjectClassEntity objectClass;

	@Column(nullable = false)
	private boolean initialDataReceived;

	protected OdpObjectEntity() {
		// for JPA
	}

	public OdpObjectEntity(OdpObjectEntity parent, String name,
			OdpObjectClassEntity objectClass) {
		this.parent = parent;
		this.name = name;
		this.objectClass = objectClass;
	}

	@Override
	public Long getId() {
		return this.id;
	}

	@Override
	public OdpObjectClassEntity getObjectClass() {
		return objectClass;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public OdpObjectEntity getParent() {
		return parent;
	}

	@Override
	public String toString() {
		return "OdpObjectEntity [id=" + id + ", name=" + name
				+ ", objectClass=" + objectClass + "]";
	}

	@PostLoad
	public void ensureInitialized() {
		getParent();
	}

	@Override
	public boolean isInitialDataReceived() {
		return initialDataReceived;
	}

	public void setInitialDataReceived(boolean initialDataReceived) {
		this.initialDataReceived = initialDataReceived;
	}
}

OpenJPA generates 

CREATE TABLE objects -- OdpObjectEntity (id BIGINT NOT NULL, 
        initial_data_received SMALLINT NOT NULL, name VARCHAR(255) NOT NULL, 
        object_class_id BIGINT NOT NULL, parent_id BIGINT NOT NULL, PRIMARY
KEY 
        (id), CONSTRAINT U_OBJECTS_NAME UNIQUE (name, parent_id)) 

I didn't expect "NOT NULL" for parent_id there. How can I avoid it? I tried
setting `columnDefinition = "BIGINT"` in parent's @Column annotation without
result. Of course, I can just not set unique constraints in the entity and
add them separately later...

--
View this message in context: http://openjpa.208410.n2.nabble.com/Why-is-NOT-NULL-generated-for-UniqueConstraint-tp7580309.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message