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