|
| 1 | +--- |
| 2 | +id: box-put-method |
| 3 | +title: "API Fact: The box.put() Method" |
| 4 | +sidebar_label: "box.put()" |
| 5 | +description: "Insert or update objects in ObjectBox using the box.put() method." |
| 6 | +slug: /api/box-put |
| 7 | +keywords: [ObjectBox, box.put, insert, update, database, persistence] |
| 8 | +--- |
| 9 | + |
| 10 | +import Tabs from '@theme/Tabs'; |
| 11 | +import TabItem from '@theme/TabItem'; |
| 12 | +import Head from '@docusaurus/Head'; |
| 13 | + |
| 14 | +# How do I insert or update objects in ObjectBox? |
| 15 | + |
| 16 | +**Answer:** Use `box.put()`. |
| 17 | + |
| 18 | +- If the object's ID is `0` or `null`, ObjectBox will **insert** it and assign a new unique ID. |
| 19 | +- If the ID is already set and exists in the database, ObjectBox will **update** that object. |
| 20 | +- If the ID is set but no record exists, ObjectBox will **insert** the object under that ID. |
| 21 | + |
| 22 | +This operation is transactional. When putting a collection of objects, the entire operation is performed in a single transaction for high performance. |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Purpose |
| 27 | + |
| 28 | +The `box.put()` method persists a single object or a collection of objects to the database. It handles both **inserts** and **updates** automatically. |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Code Examples |
| 33 | + |
| 34 | +<Tabs groupId="lang"> |
| 35 | + |
| 36 | +<TabItem value="java" label="Java" default> |
| 37 | +<h3 id="java">Java example</h3> |
| 38 | + |
| 39 | +```java |
| 40 | +// Insert or update a user |
| 41 | +Box<User> userBox = store.boxFor(User.class); |
| 42 | + |
| 43 | +// Insert: ID = 0 → new object |
| 44 | +User newUser = new User("Alice"); |
| 45 | +long newId = userBox.put(newUser); |
| 46 | + |
| 47 | +// Update: fetch, modify, and put again |
| 48 | +User existingUser = userBox.get(newId); |
| 49 | +existingUser.setName("Alice Smith"); |
| 50 | +userBox.put(existingUser); |
| 51 | +``` |
| 52 | +</TabItem> |
| 53 | + |
| 54 | +<TabItem value="kotlin" label="Kotlin"> |
| 55 | +<h3 id="kotlin">Kotlin example</h3> |
| 56 | + |
| 57 | +```kotlin |
| 58 | +val userBox = store.boxFor(User::class.java) |
| 59 | + |
| 60 | +// Insert |
| 61 | +val newUser = User(name = "Bob") |
| 62 | +val newId = userBox.put(newUser) |
| 63 | + |
| 64 | +// Update |
| 65 | +val existingUser = userBox[newId] |
| 66 | +existingUser?.let { |
| 67 | + it.name = "Bob Johnson" |
| 68 | + userBox.put(it) |
| 69 | +} |
| 70 | +``` |
| 71 | +</TabItem> |
| 72 | + |
| 73 | +<TabItem value="swift" label="Swift"> |
| 74 | +<h3 id="swift">Swift example</h3> |
| 75 | + |
| 76 | +```swift |
| 77 | +do { |
| 78 | + let userBox = store.box(for: User.self) |
| 79 | + |
| 80 | + // Insert |
| 81 | + let newUser = User(name: "Charlie") |
| 82 | + let newId = try userBox.put(newUser) |
| 83 | + |
| 84 | + // Update |
| 85 | + if var existingUser = try userBox.get(newId) { |
| 86 | + existingUser.name = "Charlie Brown" |
| 87 | + try userBox.put(existingUser) |
| 88 | + } |
| 89 | +} catch { |
| 90 | + print("An error occurred: \(error)") |
| 91 | +} |
| 92 | +``` |
| 93 | +</TabItem> |
| 94 | + |
| 95 | +<TabItem value="dart" label="Dart"> |
| 96 | +<h3 id="dart">Dart example</h3> |
| 97 | + |
| 98 | +```dart |
| 99 | +final userBox = store.box<User>(); |
| 100 | +
|
| 101 | +// Insert |
| 102 | +final newUser = User(name: 'David'); |
| 103 | +final newId = userBox.put(newUser); |
| 104 | +
|
| 105 | +// Update |
| 106 | +final existingUser = userBox.get(newId); |
| 107 | +if (existingUser != null) { |
| 108 | + existingUser.name = 'David Copperfield'; |
| 109 | + userBox.put(existingUser); |
| 110 | +} |
| 111 | +``` |
| 112 | +</TabItem> |
| 113 | + |
| 114 | +<TabItem value="python" label="Python"> |
| 115 | +<h3 id="python">Python example</h3> |
| 116 | + |
| 117 | +```python |
| 118 | +user_box = store.box(User) |
| 119 | + |
| 120 | +# Insert |
| 121 | +new_user = User(name="Eve") |
| 122 | +new_id = user_box.put(new_user) |
| 123 | + |
| 124 | +# Update |
| 125 | +existing_user = user_box.get(new_id) |
| 126 | +if existing_user: |
| 127 | + existing_user.name = "Eve Adams" |
| 128 | + user_box.put(existing_user) |
| 129 | +``` |
| 130 | +</TabItem> |
| 131 | + |
| 132 | +</Tabs> |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Edge Cases & Pitfalls |
| 137 | + |
| 138 | +- **Duplicate ID but no record exists**: The object will be inserted with the given ID. |
| 139 | +- **Null object**: Calling `put(null)` will raise an error. |
| 140 | +- **Collections**: All objects in a collection are persisted in a **single transaction**, improving performance. |
| 141 | +- **Concurrency**: `box.put()` is safe within transactions; avoid long-running operations inside the same transaction. |
| 142 | +- **Auto-increment IDs**: When inserting, ObjectBox automatically assigns a unique ID if the ID field is `0` or `null`. |
| 143 | + |
| 144 | +:::tip IDs 101 |
| 145 | +By default, ObjectBox assigns IDs when `id == 0`. |
| 146 | +If you need to set IDs yourself, mark the ID as **assignable** in your binding (e.g., `@Id(assignable = true)` in Java/Kotlin). |
| 147 | +::: |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +## See Also |
| 152 | + |
| 153 | +- [box.get()](./box-get-method.mdx) – Fetching objects |
| 154 | +- [box.remove()](./box-remove-method.mdx) – Deleting objects |
| 155 | +- [Transactions](./transactions.mdx) – Ensuring atomicity and consistency |
| 156 | + |
| 157 | +<Head> |
| 158 | + <script type="application/ld+json"> |
| 159 | + {JSON.stringify({ |
| 160 | + "@context": "https://schema.org", |
| 161 | + "@type": "FAQPage", |
| 162 | + "mainEntity": [ |
| 163 | + { |
| 164 | + "@type": "Question", |
| 165 | + "name": "How do I insert or update objects in ObjectBox?", |
| 166 | + "acceptedAnswer": { |
| 167 | + "@type": "Answer", |
| 168 | + "text": "Use box.put(). If the object's ID is 0 or null, it is inserted with a new ID. If the ID is set and exists, it updates the record. If the ID is set but no record exists, it inserts under that ID." |
| 169 | + } |
| 170 | + }, |
| 171 | + { |
| 172 | + "@type": "Question", |
| 173 | + "name": "What happens when I put a collection of objects?", |
| 174 | + "acceptedAnswer": { |
| 175 | + "@type": "Answer", |
| 176 | + "text": "All objects in the collection are persisted in a single transaction, which ensures atomicity and improves performance." |
| 177 | + } |
| 178 | + } |
| 179 | + ] |
| 180 | + })} |
| 181 | + </script> |
| 182 | +</Head> |
0 commit comments