กําหนดและค้นหาความสัมพันธ์แบบ 1:1
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ความสัมพันธ์แบบ 1:1 ระหว่างเอนทิตี 2 รายการคือความสัมพันธ์ที่อินสแตนซ์แต่ละรายการของเอนทิตีหลักสอดคล้องกับอินสแตนซ์ของเอนทิตีย่อยเพียง 1 รายการเท่านั้น และในทางกลับกัน
ตัวอย่างเช่น ลองพิจารณาแอปสตรีมมิงเพลงที่ผู้ใช้มีคลังเพลงที่เป็นของตนเอง ผู้ใช้แต่ละรายจะมีเพียงคลังเดียวและคลังแต่ละคลังจะเชื่อมโยงกับผู้ใช้เพียงรายเดียว ดังนั้น เอนทิตี User
จึงมีความสัมพันธ์แบบ 1:1 กับเอนทิตี Library
ทําตามขั้นตอนต่อไปนี้เพื่อกําหนดและคําค้นหาความสัมพันธ์แบบ 1:1 ในฐานข้อมูล
- กําหนดความสัมพันธ์: สร้างคลาสสําหรับเอนทิตีทั้ง 2 รายการ โดยตรวจสอบว่าเอนทิตีหนึ่งอ้างอิงคีย์หลักของเอนทิตีอีกรายการหนึ่ง
- ค้นหาเอนทิตี: จำลองความสัมพันธ์ในคลาสข้อมูลใหม่และสร้างเมธอดเพื่อดึงข้อมูลที่เกี่ยวข้อง
กําหนดความสัมพันธ์
หากต้องการกำหนดความสัมพันธ์แบบ 1:1 ให้สร้างคลาสสำหรับเอนทิตีแต่ละรายการก่อน เอนทิตีอย่างใดอย่างหนึ่งต้องมีตัวแปรที่อ้างอิงถึงคีย์หลักของเอนทิตีอีกรายการ
Kotlin
@Entity
data class User(
@PrimaryKey val userId: Long,
val name: String,
val age: Int
)
@Entity
data class Library(
@PrimaryKey val libraryId: Long,
val userOwnerId: Long
)
Java
@Entity
public class User {
@PrimaryKey public long userId;
public String name;
public int age;
}
@Entity
public class Library {
@PrimaryKey public long libraryId;
public long userOwnerId;
}
ค้นหาเอนทิตี
หากต้องการค้นหารายการผู้ใช้และไลบรารีที่เกี่ยวข้อง คุณต้องจำลองความสัมพันธ์แบบ 1:1 ระหว่างเอนทิตี 2 รายการก่อน
โดยสร้างคลาสข้อมูลใหม่ซึ่งแต่ละอินสแตนซ์มีอินสแตนซ์ของเอนทิตีหลักและอินสแตนซ์ที่เกี่ยวข้องของเอนทิตีย่อย เพิ่มคำอธิบายประกอบ @Relation
ลงในอินสแตนซ์ของเอนทิตีย่อย โดยตั้งค่า parentColumn
เป็นชื่อคอลัมน์คีย์หลักของเอนทิตีหลัก และตั้งค่า entityColumn
เป็นชื่อคอลัมน์ของเอนทิตีย่อยที่อ้างอิงคีย์หลักของเอนทิตีหลัก
Kotlin
data class UserAndLibrary(
@Embedded val user: User,
@Relation(
parentColumn = "userId",
entityColumn = "userOwnerId"
)
val library: Library
)
Java
public class UserAndLibrary {
@Embedded public User user;
@Relation(
parentColumn = "userId",
entityColumn = "userOwnerId"
)
public Library library;
}
สุดท้าย ให้เพิ่มเมธอดลงในคลาส DAO ซึ่งจะแสดงผลอินสแตนซ์ทั้งหมดของคลาสข้อมูลที่จับคู่เอนทิตีหลักกับเอนทิตีย่อย วิธีนี้ต้องใช้ห้องเพื่อเรียกใช้การค้นหา 2 รายการ คุณจึงควรเพิ่มหมายเหตุประกอบ @Transaction
ลงในเมธอดนี้ วิธีนี้ช่วยให้มั่นใจว่าการดำเนินการทั้งหมดจะทำงานอย่างเป็นระเบียบ
Kotlin
@Transaction
@Query("SELECT * FROM User")
fun getUsersAndLibraries(): List<UserAndLibrary>
Java
@Transaction
@Query("SELECT * FROM User")
public List<UserAndLibrary> getUsersAndLibraries();
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[null,null,["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Define and query one-to-one relationships\n\nA *one-to-one relationship* between two entities is a relationship where each\ninstance of the parent entity corresponds to exactly one instance of the child\nentity, and the reverse is also true.\n\nFor example, consider a music streaming app where the user has a library of\nsongs that they own. Each user has only one library, and each library\ncorresponds to exactly one user. Therefore, there is a one-to-one relationship\nbetween the `User` entity and the `Library` entity.\n\nFollow these steps to define and query one-to-one relationships in your\ndatabase:\n\n1. **[Define the relationship](#define)**: Create classes for both entities, ensuring one references the other's primary key.\n2. **[Query the entities](#query)**: Model the relationship in a new data class and create a method to retrieve the related data.\n\nDefine the relationship\n-----------------------\n\nTo define a one-to-one relationship, first create a class for each of your two\nentities. One of the entities must include a variable that is a reference to the\nprimary key of the other entity. \n\n### Kotlin\n\n @Entity\n data class User(\n @PrimaryKey val userId: Long,\n val name: String,\n val age: Int\n )\n\n @Entity\n data class Library(\n @PrimaryKey val libraryId: Long,\n val userOwnerId: Long\n )\n\n### Java\n\n @Entity\n public class User {\n @PrimaryKey public long userId;\n public String name;\n public int age;\n }\n\n @Entity\n public class Library {\n @PrimaryKey public long libraryId;\n public long userOwnerId;\n }\n\nQuery the entities\n------------------\n\nTo query the list of users and corresponding libraries, you must first model the\none-to-one relationship between the two entities.\n\nTo do this, create a new data class where each instance holds an instance of the\nparent entity and the corresponding instance of the child entity. Add the\n[`@Relation`](/reference/kotlin/androidx/room/Relation) annotation to the instance of the child entity, with\n[`parentColumn`](/reference/kotlin/androidx/room/Relation#parentcolumn()) set to the name of the primary key column of the parent\nentity and [`entityColumn`](/reference/kotlin/androidx/room/Relation#entitycolumn()) set to the name of the column of the child entity\nthat references the parent entity's primary key. \n\n### Kotlin\n\n data class UserAndLibrary(\n @Embedded val user: User,\n @Relation(\n parentColumn = \"userId\",\n entityColumn = \"userOwnerId\"\n )\n val library: Library\n )\n\n### Java\n\n public class UserAndLibrary {\n @Embedded public User user;\n @Relation(\n parentColumn = \"userId\",\n entityColumn = \"userOwnerId\"\n )\n public Library library;\n }\n\nFinally, add a method to the DAO class that returns all instances of the data\nclass that pairs the parent entity and the child entity. This method requires\nRoom to run two queries. You should therefore add the [`@Transaction`](/reference/kotlin/androidx/room/Transaction)\nannotation to this method. This ensures that the whole operation runs\natomically. \n\n### Kotlin\n\n @Transaction\n @Query(\"SELECT * FROM User\")\n fun getUsersAndLibraries(): List\u003cUserAndLibrary\u003e\n\n### Java\n\n @Transaction\n @Query(\"SELECT * FROM User\")\n public List\u003cUserAndLibrary\u003e getUsersAndLibraries();"]]