{"id":18429069,"url":"https://github.com/openliberty/sample-jakarta-data","last_synced_at":"2025-04-07T17:32:32.710Z","repository":{"id":185361663,"uuid":"653254737","full_name":"OpenLiberty/sample-jakarta-data","owner":"OpenLiberty","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-15T20:04:55.000Z","size":469,"stargazers_count":3,"open_issues_count":5,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-17T05:46:57.876Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OpenLiberty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-13T17:38:34.000Z","updated_at":"2024-10-15T20:04:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"5b09ae78-af02-40ee-89c2-277b274a6468","html_url":"https://github.com/OpenLiberty/sample-jakarta-data","commit_stats":null,"previous_names":["openliberty/sample-jakarta-data"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLiberty%2Fsample-jakarta-data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLiberty%2Fsample-jakarta-data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLiberty%2Fsample-jakarta-data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenLiberty%2Fsample-jakarta-data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OpenLiberty","download_url":"https://codeload.github.com/OpenLiberty/sample-jakarta-data/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223286329,"owners_count":17120002,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T05:15:41.836Z","updated_at":"2024-11-06T05:15:42.591Z","avatar_url":"https://github.com/OpenLiberty.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](https://raw.githubusercontent.com/OpenLiberty/open-liberty/release/logos/logo_horizontal_light_navy.png)\n\n# Jakarta Data Sample\nThis sample shows how to store and retrieve data using Jakarta Data. Jakarta Data is planned to release in Jakarta EE 11, and is currently in beta in Open Liberty.\n\n## Environment Setup\nTo run this sample, first [download](https://github.com/OpenLiberty/sample-jakarta-data/archive/main.zip) or clone this repo - to clone:\n```\ngit clone git@github.com:OpenLiberty/sample-jakarta-data.git\n```\n\n### Set Up PostgreSQL\nYou will need a PostgreSQL instance to use this sample. If you have Docker installed, you can run the following from inside the `sample-jakarta-data` directory:\n\n```\ndocker build -t liberty_postgres postgres\ndocker run --name liberty_postgres -d -p 5432:5432 liberty_postgres\n```\nIf you are not using Docker, you will need to create a user with the name `sampleUser` and password `openliberty` with access to a database named `testdb`\n\n## Running the Sample\nFrom inside the `sample-jakarta-data` directory, build and start the application in Open Liberty with the following command:\n```\n./mvnw liberty:dev\n```\n\nOnce the server has started, the application is availible at http://localhost:9080\n\n### Try it out\nGive the sample a try by registering a crew member. Enter a name (a **String**), an ID Number (an **Integer**), and select a Rank from the menu, then click 'Register Crew Member'.\n\nThe new crew member will appear in the **Crew Members** box. Continue to add crew members, choosing a variety of ranks for the crew members. In the Queries box, click the findByRank button. The crew members will appear in a **Crew Members by Rank** box, sorted into columns by rank.\n\n### How it works\nThis application provides a few REST endpoints to demonstrate some of the capabilities of Jakarta Data. \nThere are two classes which are used for Jakarta Data, a Jakarta Persistence Entity ([CrewMember](src/main/java/io/openliberty/sample/application/CrewMember.java)) and a Jakarta Data Repository ([CrewMembers](src/main/java/io/openliberty/sample/application/CrewMembers.java)). **CrewMembers** is annotated with `@Repository` and extends the Jakarta Data **DataRepository** interface, adding save, delete, and query methods. When the repository is injected into another object using CDI, Jakarta Data will provide an implementation of the interface, including implementations of the save, delete, and query methods.\n\nThe **CrewMembers** repository is injected into the REST application using CDI\n\n```java\npublic class CrewService {\n//[...]\n@Inject\nCrewMembers crewMembers;\n```\n\nThe first endpoint persists a **CrewMember** in the database by calling `crewMembers.save()`\n\n```java\npublic String add(CrewMember crewMember) {\n    crewMembers.save(crewMember);\n\n    //Jakarta Validation[...]\n```\n\nTo remove an individual **CrewMember** from the database based on the ID, you can use `crewMembers.deleteByCrewID`\n```java\npublic void remove(@PathParam(\"id\") int id) {\n    crewMembers.deleteByCrewID(id);\n```\n\nIn order to display all of our **CrewMember**s, you can get all of them easily by calling `crewMembers.findAll()`\n```java\npublic String retrieve() {\n    Iterable\u003cCrewMember\u003e crewMembersIterable = crewMembers.findAll()::iterator;\n```\nIn the `CrewMembers.java` file we can see that these will be returned sorted alphabetically, using `@OrderBy(\"name\")`\n```java\npublic interface CrewMembers {\n//[...]\n@Find\n@OrderBy(\"name\")\nStream\u003cCrewMember\u003e findAll();\n```\n\nFinally, for a slightly more complex operation, we can ask for a subset of the crew members with a given **Rank**, using `crewMembers.findByRank()`\n```java\npublic String retrieveByRank(@PathParam(\"rank\") String rank) {\n    List\u003cCrewMember\u003e crewMembersList = crewMembers.findByRank(Rank.fromString(rank));\n```\n\n### Data Source configuration\n\nThe application makes use of Open Liberty's built in Jakarta Data implementation, backed by Jakarta Persistence. The connection to the database is defined as a **DataSource**, which is configured in the [server.xml](src/main/liberty/config/server.xml).\n\n## Stop Postgres\nWhen you are done trying out the sample application, you can stop the Postgres container with:\n```\ndocker stop liberty_postgres\n```\n\n## Where to go next\n\nCheck out the Jakarta Data Specification on GitHub: https://github.com/jakartaee/data.\nYou can make suggestions or report bugs by opening an issue, or star the repository to show you're interested.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenliberty%2Fsample-jakarta-data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenliberty%2Fsample-jakarta-data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenliberty%2Fsample-jakarta-data/lists"}