如何使用接口和 JPA

发布于 2024-12-28 12:17:12 字数 1173 浏览 1 评论 0原文

首先我应该说,我对 Java EE 还很陌生,而且我在 Java 方面还没有很强的理论背景。

我无法掌握如何在 Java 中将 JPAinterfaces 一起使用。为了说明我发现的困难,我创建了一个非常简单的例子。

如果我有两个简单的接口 PersonPet

public interface Person
{
    public Pet getPet();
    public void setPet(Pet pet);
}

public interface Pet
{
    public String getName();
}

以及一个实现 PersonPerson 的实体 PersonEntity实现 Pet 的 code>PetEntity:

@Entity
public class PersonEntity implements Person
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private PetEntity pet;

    @Override
    public void setPet(Pet pet)
    {
        /* How do i solve this? */
    }
}

@Entity
public class PetEntity implements Pet
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    /* Getters and Setters omitted */

}

如何正确处理 setPet 方法中的情况,在该方法中我想要保留上述两个实体之间的关系?

我想使用接口的主要原因是因为我想保持模块/层之间对公共接口的依赖关系。我还能如何避免从例如我的 ManagedBean 直接获得实体的依赖关系?

如果有人建议不要在实体上使用接口,那么请解释有哪些替代方法或模式。

I should start out by saying that I am fairly new to Java EE and that I do not have a strong theoretical background in Java yet.

I'm having trouble grasping how to use JPA together with interfaces in Java. To illustrate what I find hard I created a very simple example.

If I have two simple interfaces Person and Pet:

public interface Person
{
    public Pet getPet();
    public void setPet(Pet pet);
}

public interface Pet
{
    public String getName();
}

And an Entity PersonEntity which implements Person as well as a PetEntity which implements Pet:

@Entity
public class PersonEntity implements Person
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private PetEntity pet;

    @Override
    public void setPet(Pet pet)
    {
        /* How do i solve this? */
    }
}

@Entity
public class PetEntity implements Pet
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    /* Getters and Setters omitted */

}

How do I properly handle the case in the setPet method in which I want to persist the relationships between the two entities above?

The main reason I want to use interfaces is because I want to keep dependencies between modules/layers to the public interfaces. How else do I avoid getting a dependency from e.g. my ManagedBean directly to an Entity?

If someone recommends against using interfaces on entities, then please explain what alternatives methods or patterns there are.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悍妇囚夫 2025-01-04 12:17:12

您可以使用 targetEntity 关系注释中的属性。

@Entity
public class PersonEntity implements Person {
    private Long id;

    private Pet pet;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

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

    @Override
    @OneToOne(targetEntity = PetEntity.class)
    public Pet getPet() {
        return pet;
    }        

    public void setPet(Pet pet) {
        this.pet = pet;
    }
}

You can use targetEntity property in the relationship annotation.

@Entity
public class PersonEntity implements Person {
    private Long id;

    private Pet pet;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

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

    @Override
    @OneToOne(targetEntity = PetEntity.class)
    public Pet getPet() {
        return pet;
    }        

    public void setPet(Pet pet) {
        this.pet = pet;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文