এক-থেকে-অনেক সম্পর্ককে সংজ্ঞায়িত করুন এবং জিজ্ঞাসা করুন, এক-থেকে-অনেক সম্পর্কের সংজ্ঞা দিন এবং অনুসন্ধান করুন
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
দুটি সত্তার মধ্যে এক থেকে একাধিক সম্পর্ক এমন একটি সম্পর্ক যেখানে পিতামাতার সত্তার প্রতিটি দৃষ্টান্ত শিশু সত্তার শূন্য বা তার বেশি দৃষ্টান্তের সাথে মিলে যায়, তবে শিশু সত্তার প্রতিটি দৃষ্টান্ত শুধুমাত্র পিতামাতার সত্তার ঠিক একটি উদাহরণের সাথে মিলে যেতে পারে৷
মিউজিক স্ট্রিমিং অ্যাপের উদাহরণে, ধরুন ব্যবহারকারীর প্লেলিস্টে তাদের গান সাজানোর ক্ষমতা আছে। প্রতিটি ব্যবহারকারী যত খুশি প্লেলিস্ট তৈরি করতে পারে, কিন্তু ঠিক একজন ব্যবহারকারী প্রতিটি প্লেলিস্ট তৈরি করে। অতএব, User
সত্তা এবং Playlist
সত্তার মধ্যে একটি থেকে একাধিক সম্পর্ক রয়েছে৷
আপনার ডাটাবেসে এক-থেকে-অনেক সম্পর্কগুলিকে সংজ্ঞায়িত করতে এবং অনুসন্ধান করতে এই পদক্ষেপগুলি অনুসরণ করুন:
- সম্পর্ক সংজ্ঞায়িত করুন : উভয় সত্তার জন্য ক্লাস তৈরি করুন, শিশু সত্তাটি পিতামাতার প্রাথমিক কী উল্লেখ করে।
- সত্তাগুলিকে জিজ্ঞাসা করুন : একটি নতুন ডেটা ক্লাসে সম্পর্কের মডেল করুন এবং সম্পর্কিত ডেটা পুনরুদ্ধার করার জন্য একটি পদ্ধতি প্রয়োগ করুন৷
সম্পর্কের সংজ্ঞা দাও
এক-থেকে-অনেক সম্পর্ক সংজ্ঞায়িত করতে, প্রথমে দুটি সত্তার জন্য একটি শ্রেণী তৈরি করুন। এক-এক সম্পর্কের মতো, শিশু সত্তাকে অবশ্যই একটি পরিবর্তনশীল অন্তর্ভুক্ত করতে হবে যা পিতামাতার সত্তার প্রাথমিক কীর একটি রেফারেন্স।
কোটলিন
@Entity
data class User(
@PrimaryKey val userId: Long,
val name: String,
val age: Int
)
@Entity
data class Playlist(
@PrimaryKey val playlistId: Long,
val userCreatorId: Long,
val playlistName: String
)
জাভা
@Entity
public class User {
@PrimaryKey public long userId;
public String name;
public int age;
}
@Entity
public class Playlist {
@PrimaryKey public long playlistId;
public long userCreatorId;
public String playlistName;
}
সত্তা জিজ্ঞাসা
ব্যবহারকারীদের তালিকা এবং সংশ্লিষ্ট প্লেলিস্টগুলি জিজ্ঞাসা করতে, আপনাকে প্রথমে দুটি সত্তার মধ্যে এক থেকে একাধিক সম্পর্কের মডেল করতে হবে
এটি করার জন্য, একটি নতুন ডেটা ক্লাস তৈরি করুন যেখানে প্রতিটি উদাহরণে পিতামাতার সত্তার একটি দৃষ্টান্ত এবং সমস্ত সংশ্লিষ্ট চাইল্ড সত্তা দৃষ্টান্তগুলির একটি তালিকা থাকে৷ সন্তান সত্তার উদাহরণে @Relation
টীকা যোগ করুন, parentColumn
এর সাথে প্যারেন্ট এন্টিটির প্রাথমিক কী কলামের নামের সাথে সেট করা হয়েছে এবং entityColumn
চাইল্ড এন্টিটির কলামের নামের সাথে সেট করা হয়েছে যা পিতামাতার সত্তার প্রাথমিক কীকে উল্লেখ করে।
কোটলিন
data class UserWithPlaylists(
@Embedded val user: User,
@Relation(
parentColumn = "userId",
entityColumn = "userCreatorId"
)
val playlists: List<Playlist>
)
জাভা
public class UserWithPlaylists {
@Embedded public User user;
@Relation(
parentColumn = "userId",
entityColumn = "userCreatorId"
)
public List<Playlist> playlists;
}
অবশেষে, DAO ক্লাসে একটি পদ্ধতি যোগ করুন যা ডেটা ক্লাসের সমস্ত দৃষ্টান্ত প্রদান করে যা পিতামাতার সত্তা এবং শিশু সত্তাকে জোড়া দেয়। এই পদ্ধতিতে দুটি প্রশ্ন চালানোর জন্য রুম প্রয়োজন, তাই এই পদ্ধতিতে @Transaction
টীকা যোগ করুন যাতে পুরো অপারেশনটি পারমাণবিকভাবে সঞ্চালিত হয়।
কোটলিন
@Transaction
@Query("SELECT * FROM User")
fun getUsersWithPlaylists(): List<UserWithPlaylists>
জাভা
@Transaction
@Query("SELECT * FROM User")
public List<UserWithPlaylists> getUsersWithPlaylists();
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি 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-many relationships\n\nA *one-to-many relationship* between two entities is a relationship where each\ninstance of the parent entity corresponds to zero or more instances of the child\nentity, but each instance of the child entity can only correspond to exactly one\ninstance of the parent entity.\n\nIn the music streaming app example, suppose the user has the ability to organize\ntheir songs into playlists. Each user can create as many playlists as they want,\nbut exactly one user creates each playlist. Therefore, there is a one-to-many\nrelationship between the `User` entity and the `Playlist` entity.\n\nFollow these steps to define and query one-to-many relationships in your\ndatabase:\n\n1. **[Define the relationship](#define)**: Create classes for both entities, with the child entity referencing the parent's primary key.\n2. **[Query the entities](#query)**: Model the relationship in a new data class and implement a method to retrieve the related data.\n\nDefine the relationship\n-----------------------\n\nTo define a one-to-many relationship, first create a class for the two entities.\nAs in a one-to-one relationship, the child entity must include a variable that\nis a reference to the primary key of the parent 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 Playlist(\n @PrimaryKey val playlistId: Long,\n val userCreatorId: Long,\n val playlistName: String\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 Playlist {\n @PrimaryKey public long playlistId;\n public long userCreatorId;\n public String playlistName;\n }\n\nQuery the entities\n------------------\n\nTo query the list of users and corresponding playlists, you must first model the\none-to-many 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 a list of all corresponding child entity instances. 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 UserWithPlaylists(\n @Embedded val user: User,\n @Relation(\n parentColumn = \"userId\",\n entityColumn = \"userCreatorId\"\n )\n val playlists: List\u003cPlaylist\u003e\n )\n\n### Java\n\n public class UserWithPlaylists {\n @Embedded public User user;\n @Relation(\n parentColumn = \"userId\",\n entityColumn = \"userCreatorId\"\n )\n public List\u003cPlaylist\u003e playlists;\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, so add the [`@Transaction`](/reference/kotlin/androidx/room/Transaction) annotation to this\nmethod so that the whole operation is performed atomically. \n\n### Kotlin\n\n @Transaction\n @Query(\"SELECT * FROM User\")\n fun getUsersWithPlaylists(): List\u003cUserWithPlaylists\u003e\n\n### Java\n\n @Transaction\n @Query(\"SELECT * FROM User\")\n public List\u003cUserWithPlaylists\u003e getUsersWithPlaylists();"]]