Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungThe Indie Dev Visibility Playbook: From Zero Users to Your First 100(21.09.2026 um 00:08 Uhr)
Sichere ProgrammierungBase, Chat and Reasoning Models: How Are They Different?(21.09.2026 um 00:09 Uhr)
Sichere ProgrammierungHexfield Deck is for Kanban lovers and Markdown believers(21.09.2026 um 00:20 Uhr)
Sichere ProgrammierungPermissions and Authorisation: A Practical Playbook(21.09.2026 um 00:21 Uhr)
Linux Tipps & Hardeningfilet | Terminal File Manager(20.09.2026 um 21:03 Uhr)
Linux Tipps & HardeningLooking for feedback on my Linux Distro(20.09.2026 um 21:03 Uhr)
Sichere ProgrammierungThe Indie Dev Visibility Playbook: From Zero Users to Your First 100(21.09.2026 um 00:08 Uhr)
Sichere ProgrammierungBase, Chat and Reasoning Models: How Are They Different?(21.09.2026 um 00:09 Uhr)
Sichere ProgrammierungHexfield Deck is for Kanban lovers and Markdown believers(21.09.2026 um 00:20 Uhr)
Sichere ProgrammierungPermissions and Authorisation: A Practical Playbook(21.09.2026 um 00:21 Uhr)
Linux Tipps & Hardeningfilet | Terminal File Manager(20.09.2026 um 21:03 Uhr)
Linux Tipps & HardeningLooking for feedback on my Linux Distro(20.09.2026 um 21:03 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Understanding JPA One-to-One Relationships in Java

Reagiere als Erste:r — dein Feedback zählt!

When developing Java applications with Spring Boot and JPA (Java Persistence API), understanding how to map relationships between entities is crucial. In this article, we'll focus on the one-to-one relationship, explaining its concept, providing real-world scenarios where it's appropriate, and demonstrating how to implement it in code.

What is a One-to-One Relationship?
A one-to-one relationship in a database means that a row in one table is linked to exactly one row in another table and vice versa. This is useful when you have two entities that are closely related and you want to keep them in separate tables for modularity, performance, or design reasons.

When to Use a One-to-One Relationship?
One-to-one relationships are appropriate in scenarios where an entity should be uniquely associated with another entity. Here are some practical examples:

User and Profile: A user can have one profile containing additional details like address, profile picture, etc.
Post and PostDetail: A blog post can have one post detail entity containing metadata such as tags, view count, etc.
Employee and ParkingSpot: An employee can be assigned one parking spot.
Example Scenario: User and Profile
Let's consider a common scenario where each user in a system has a unique profile. This separation allows us to manage user authentication details separately from their personal information.

Step-by-Step Implementation
Step 1: Define the Entities
First, create the User and Profile entities. These classes will be annotated with JPA annotations to define the relationship.

User.java:

java
Copy code
package com.example.demo.model;

import javax.persistence.*;

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String username;

private String password;

@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private Profile profile;

// Getters and setters

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public Profile getProfile() {
    return profile;
}

public void setProfile(Profile profile) {
    this.profile = profile;
    profile.setUser(this); // Bidirectional synchronization
}

}
Profile.java:

java
Copy code
package com.example.demo.model;

import javax.persistence.*;

@Entity
public class Profile {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String address;

private String phoneNumber;

@OneToOne
@JoinColumn(name = "user_id")
private User user;

// Getters and setters

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

}
Step 2: Create Repositories
Next, create Spring Data JPA repositories for the User and Profile entities.

UserRepository.java:

java
Copy code
package com.example.demo.repository;

import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository {
}
ProfileRepository.java:

java
Copy code
package com.example.demo.repository;

import com.example.demo.model.Profile;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProfileRepository extends JpaRepository {
}
Step 3: Create Service Layer
Create a service to manage users and profiles.

UserService.java:

java
Copy code
package com.example.demo.service;

import com.example.demo.model.Profile;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class UserService {

@Autowired
private UserRepository userRepository;

@Transactional
public User createUserWithProfile(String username, String password, String address, String phoneNumber) {
    User user = new User();
    user.setUsername(username);
    user.setPassword(password);

    Profile profile = new Profile();
    profile.setAddress(address);
    profile.setPhoneNumber(phoneNumber);

    user.setProfile(profile);
    userRepository.save(user);

    return user;
}

public User getUser(Long id) {
    return userRepository.findById(id).orElse(null);
}

}
Step 4: Create Controller
Create a REST controller to expose the user and profile management endpoints.

UserController.java:

java
Copy code
package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

@Autowired
private UserService userService;

@PostMapping
public User createUser(@RequestParam String username, @RequestParam String password,
                       @RequestParam String address, @RequestParam String phoneNumber) {
    return userService.createUserWithProfile(username, password, address, phoneNumber);
}

@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
    return userService.getUser(id);
}

}
Testing with Postman
Create a User with Profile:

Endpoint: POST /users
Params: username, password, address, phoneNumber
Example Request:
http
Copy code
POST http://localhost:5050/users
Content-Type: application/x-www-form-urlencoded

username=JohnDoe&password=secret&address=123+Main+St&phoneNumber=123-456-7890
Retrieve a User with Profile:

Endpoint: GET /users/{id}
Example Request:
http
Copy code
GET http://localhost:5050/users/1
Conclusion
A one-to-one relationship in JPA is used when each instance of an entity is associated with exactly one instance of another entity. This is useful for closely related data that should be stored separately for design or performance reasons. In this article, we've walked through creating a one-to-one relationship between User and Profile entities, showcasing how to implement it and test it using Postman. By understanding and utilizing such relationships, you can design more efficient and modular applications.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding JPA One-to-One Relationships in Java

Thematisch verwandte Begriffe: Understanding, OnetoOne, Relationships, Java · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94084 | Suricata before 8.0.7 has an Http2ThreadMultiBuf use-after-free when a t…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick