Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.objectbox.relation;
import java.io.Serializable;
import java.lang.reflect.Field;
import javax.annotation.Nullable;
import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.Cursor;
import io.objectbox.annotation.Backlink;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.apihint.Internal;
import io.objectbox.exception.DbDetachedException;
import io.objectbox.internal.ReflectionCache;
/**
* A to-one relation of an entity that references one object of a {@link TARGET} entity.
*
* Example:
*
{@code
* // Java
* @Entity
* public class Order {
* private ToOne customer;
* }
*
* // Kotlin
* @Entity
* data class Order() {
* lateinit var customer: ToOne
* }
* }
*
* Uses lazy initialization. The target object ({@link #getTarget()}) is only read from the database when it is first
* accessed.
*
* Common usage:
*
*
Set the target object with {@link #setTarget} to create a relation.
* When the object with the ToOne is put, if the target object is new (its ID is 0), it will be put as well.
* Otherwise, only the target ID in the database is updated.
*
{@link #setTargetId} of the target object to create a relation.
*
{@link #setTarget} with {@code null} or {@link #setTargetId} to {@code 0} to remove the relation.
*
*
* Then, to persist the changes {@link Box#put} the object with the ToOne.
*
*
{@code
* // Example 1: create a relation
* order.getCustomer().setTarget(customer);
* // or order.getCustomer().setTargetId(customerId);
* store.boxFor(Order.class).put(order);
*
* // Example 2: remove the relation
* order.getCustomer().setTarget(null);
* // or order.getCustomer().setTargetId(0);
* store.boxFor(Order.class).put(order);
* }
*
* The target object is referenced by its ID.
* This target ID ({@link #getTargetId()}) is persisted as part of the object with the ToOne in a special
* property created for each ToOne (named like "customerId").
*
* To get all objects with a ToOne that reference a target object, see {@link Backlink}.
*
* @param target object type ({@link Entity @Entity} class).
*/
// TODO not exactly thread safe
public class ToOne implements Serializable {
private static final long serialVersionUID = 5092547044335989281L;
private final Object entity;
private final RelationInfo