Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public final class Ref<T>
- Object
- Ref
A to-one relationship of a record entity, read on first use.
A record cannot be changed after it is built, so it cannot hold a lazy
relationship the way an enhanced class does. It holds a Ref instead:
import com.codename1.annotations.Entity;
import com.codename1.annotations.Id;
import com.codename1.annotations.db.ManyToOne;
import com.codename1.orm.Ref;
@Entity
record Customer(@Id long id, String name) {}
@Entity
record Order(@Id long id, @ManyToOne Ref<Customer> customer) {}
Order order = new Order(1, Ref.of(new Customer(7, "Ada")));
String name = order.customer().get().name();
An order the session reads holds an unread Ref: order.customer().id()
answers the stored key without a query, and get() reads the customer the
first time it’s called.
A Ref loaded by a session reads its target through that session, and only
while it is open: get() on a Ref that was never read, once the session is
closed, throws LazyInitializationException. id() and isPresent() read
nothing either, with one exception: on the inverse side of a one-to-one,
@OneToOne(mappedBy = ...), the record’s own row stores no key, so they read
the target, and throw the same way once the session is gone.
To point a record at another entity, build it with Ref.of(target), or
Ref.empty() for none. Two refs are equal when they point at the same
entity: the same target type and the same identifier.
Methods
public static <T> Ref<T> of(T target) | A ref to target, or an empty one when target is null. |
public static <T> Ref<T> empty() | A ref to nothing. |
public T get() | The target, read through the owning session if it has not been read yet; null when the ref is empty. |
public boolean isPresent() | True when the ref points at an entity, read or not. |
public Object id() | The target’s identifier, without reading it unless the ref is the inverse side of a one-to-one; null for an empty ref, or a target that has not been saved yet. |
public boolean isLoaded() | True once the target has been read, or for a ref that was built with a value. |
public boolean equals(Object other) | Indicates whether some other object is “equal to” this one. |
public int hashCode() | Returns a hash code value for the object. |
public String toString() | Returns a string representation of the object. |
protected final void load() | Reads the value through the owning session if it has not been read. |
protected final Object current() | The value as it stands, without reading it. |
protected final void assign(Object target) | Sets a value that needs no reading. |
protected final boolean loaded() | |
protected final Object identifier() | The identifier of the target, read or not, or null without one. |
protected final boolean unreadable() | True for the inverse side of a to-one that was never read and can no longer be: its identifier is unknown for good. |
protected final Object storedIdentifier() | The identifier as far as it is known, without reading anything. |
protected final Class targetType() | The target’s type: the bound relationship’s, or the value’s own. |
Inherited methods
Method details
of
public static <T> Ref<T> of(T target)target, or an empty one when target is null.empty
public static <T> Ref<T> empty()get
public T get()Throws
com.codename1.orm.session.LazyInitializationException- if the target was never read and the session that loaded the ref is closed
isPresent
public boolean isPresent()id
public Object id()isLoaded
public boolean isLoaded()equals
public boolean equals(Object other)hashCode
public int hashCode()toString
public String toString()load
protected final void load()current
protected final Object current()assign
protected final void assign(Object target)loaded
protected final boolean loaded()identifier
protected final Object identifier()unreadable
protected final boolean unreadable()storedIdentifier
protected final Object storedIdentifier()targetType
protected final Class targetType()