এক-এক সম্পর্ককে সংজ্ঞায়িত করুন এবং জিজ্ঞাসা করুন
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
দুটি সত্তার মধ্যে এক-এক সম্পর্ক এমন একটি সম্পর্ক যেখানে পিতামাতার প্রতিটি দৃষ্টান্ত শিশু সত্তার ঠিক একটি উদাহরণের সাথে মিলে যায় এবং বিপরীতটিও সত্য।
উদাহরণস্বরূপ, একটি মিউজিক স্ট্রিমিং অ্যাপ বিবেচনা করুন যেখানে ব্যবহারকারীর নিজস্ব গানের একটি লাইব্রেরি রয়েছে। প্রতিটি ব্যবহারকারীর শুধুমাত্র একটি লাইব্রেরি আছে, এবং প্রতিটি লাইব্রেরি ঠিক একজন ব্যবহারকারীর সাথে মিলে যায়। অতএব, User
সত্তা এবং Library
সত্তার মধ্যে এক-এক সম্পর্ক রয়েছে।
আপনার ডাটাবেসে এক-এক সম্পর্ককে সংজ্ঞায়িত করতে এবং অনুসন্ধান করতে এই পদক্ষেপগুলি অনুসরণ করুন:
- সম্পর্ক সংজ্ঞায়িত করুন : উভয় সত্ত্বার জন্য ক্লাস তৈরি করুন, নিশ্চিত করুন যে একটি অন্যটির প্রাথমিক কী উল্লেখ করে।
- সত্তাকে জিজ্ঞাসা করুন : একটি নতুন ডেটা ক্লাসে সম্পর্কের মডেল করুন এবং সম্পর্কিত ডেটা পুনরুদ্ধার করার জন্য একটি পদ্ধতি তৈরি করুন।
সম্পর্কের সংজ্ঞা দাও
এক-এক সম্পর্ককে সংজ্ঞায়িত করতে, প্রথমে আপনার দুটি সত্তার জন্য একটি শ্রেণী তৈরি করুন। সত্তাগুলির মধ্যে একটিতে অবশ্যই একটি পরিবর্তনশীল অন্তর্ভুক্ত থাকতে হবে যা অন্য সত্তার প্রাথমিক কীর একটি রেফারেন্স।
কোটলিন
@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
)
জাভা
@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;
}
সত্তা জিজ্ঞাসা
ব্যবহারকারীদের তালিকা এবং সংশ্লিষ্ট লাইব্রেরি অনুসন্ধান করতে, আপনাকে প্রথমে দুটি সত্তার মধ্যে এক-এক সম্পর্ক মডেল করতে হবে।
এটি করার জন্য, একটি নতুন ডেটা ক্লাস তৈরি করুন যেখানে প্রতিটি দৃষ্টান্তে পিতামাতার সত্তার একটি উদাহরণ এবং শিশু সত্তার সংশ্লিষ্ট দৃষ্টান্ত রয়েছে৷ সন্তান সত্তার উদাহরণে @Relation
টীকা যোগ করুন, parentColumn
এর সাথে প্যারেন্ট এন্টিটির প্রাথমিক কী কলামের নামের সাথে সেট করা হয়েছে এবং entityColumn
চাইল্ড এন্টিটির কলামের নামের সাথে সেট করা হয়েছে যা পিতামাতার সত্তার প্রাথমিক কীকে উল্লেখ করে।
কোটলিন
data class UserAndLibrary(
@Embedded val user: User,
@Relation(
parentColumn = "userId",
entityColumn = "userOwnerId"
)
val library: Library
)
জাভা
public class UserAndLibrary {
@Embedded public User user;
@Relation(
parentColumn = "userId",
entityColumn = "userOwnerId"
)
public Library library;
}
অবশেষে, DAO ক্লাসে একটি পদ্ধতি যোগ করুন যা ডেটা ক্লাসের সমস্ত দৃষ্টান্ত প্রদান করে যা প্যারেন্ট এন্টিটি এবং চাইল্ড সত্তাকে জোড়া দেয়। এই পদ্ধতিতে দুটি প্রশ্ন চালানোর জন্য রুম প্রয়োজন। তাই এই পদ্ধতিতে আপনার @Transaction
টীকা যোগ করা উচিত। এটি নিশ্চিত করে যে পুরো অপারেশনটি পারমাণবিকভাবে চলে।
কোটলিন
@Transaction
@Query("SELECT * FROM User")
fun getUsersAndLibraries(): List<UserAndLibrary>
জাভা
@Transaction
@Query("SELECT * FROM User")
public List<UserAndLibrary> getUsersAndLibraries();
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-07-29 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();"]]