Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何设置JPA实体类?_Java_Jpa - Fatal编程技术网

Java 如何设置JPA实体类?

Java 如何设置JPA实体类?,java,jpa,Java,Jpa,我正在努力解决如何设置我的JPA实体类以及哪些注释应该放在哪里 我有以下表格: Table Customer { id: primary key, name } Table CustomerDimension { id: primary key, foreign key(Customer.id), detail } 目前,我拥有以下实体类: public class Customer { @Id @GeneratedValue(strategy

我正在努力解决如何设置我的JPA实体类以及哪些注释应该放在哪里

我有以下表格:

Table Customer {
    id: primary key,
    name
}

Table CustomerDimension {
    id: primary key, foreign key(Customer.id),
    detail
}
目前,我拥有以下实体类:

public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    @Column(name = "name")
    private String name;
    @OneToOne
    private CustomerDimension customerDimension;
}

public class CustomerDimension {
    // ? what is meant to go here?
    private long id;
    @Column(name = "detail")
    private String detail;
}
要在
CustomerDimension.id
上插入具有新CustomerDimension的新客户,需要进行什么注释

CustomerDimension
是否也应该引用回
Customer

Table Customer {
    id: primary key,
    name
}

Table CustomerDimension {
    id: primary key, 
    foreign key(Customer.id),
    detail
}
CustomerDimension
是拥有方。因此,
@OneToOne
映射应该如下

public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    @Column(name = "name")
    private String name;
}

public class CustomerDimension {
    @Id
    private long id;
    @Column(name = "detail")
    private String detail;

     @OneToOne
   private Customer customer;
}

您有以下问题:

  • Customer和CustomerDimension需要注释
    @Entity
  • 在DDL中,表
    CustomerDimension
    Customer
    上有一个外键。因此,
    @OneToOne
    关系应该在
    CustomerDimension
    一侧声明
  • 仍然在DDL中,外键没有显式名称。我将假定它是
    customer\u id
    ,并使用它来声明
    @JoinColumn
    (见下文)
  • @Column
    只有在需要列的名称与属性的名称不同时,才需要注释(但为了清晰起见,可以保留注释)
下面是我将如何绘制它的地图

@Entity
@Table(name = "Customer") //Optional
public class Customer {

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

  @Column(name = "name") //Optional
  private String name;
}
对于
客户尺寸

@Entity
@Table(name = "CustomerDimension") //Optional
public class CustomerDimension {

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

  @Column(name = "detail") //Optional
  private String detail;

  @OneToOne
  @JoinColumn(name = "customer_id") //NOT optional
  private Customer customer
}
编辑(回答您的评论):

如果您确实希望FK成为主键,可以这样做:

@Entity
@Table(name = "CustomerDimension") //Optional
public class CustomerDimension {

  @Column(name = "detail") //Optional
  private String detail;

  @Id
  @OneToOne
  @JoinColumn(name = "id") //NOT optional
  private Customer customer
}

我仍然想知道你为什么不把所有信息放在同一张表中。这将为您节省一个SQL联接。

这里您拥有的是一个带有外键的
OneToMany
biidirection关系,而不是联接表。连接表似乎是供应商的首选,但它没有问题

因此,您在
Customer
中有一个
CustomerDimensions
列表(或集合),但带有
mappedBy
值集

public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy="customer")
    List<CustomerDimensions> dimensions;
}
客户
有一组维度是很自然的。通过双向映射,如果您有一个维度,那么您可以轻松地查找客户(只需参考客户字段)


编辑:由于
CustomerDimension
表有一个
Customer
id引用,因此可以为一个
Customer
选择多个
CustomerDimensions
,从而形成一个
OneToMany
关系。要设置
CustomerDimension.customer_id
字段,只需在
Customers
维度列表中添加
CustomerDimension

只要把
@Id
,因为你有一个单向的relationship@Ramanlfc-这不是一种双向关系吗,我可以从CustomerDimension->Customer开始,反之亦然?您还需要在这两个类上添加注释
@Entity
@Table
。对于第二个类,您还需要@GeneratedValue,您应该通过一些基本教程来了解exmaplesyou已经得到了与您的模式要求完全相反的映射我觉得我可能给出了错误的表定义
Customer
是根对象(上下文/主题,如果您愿意)
CustomerDimension
只是
Customer
的额外信息,因此我希望从
Customer
实体获得
CustomerDimension
实体。此外,不会生成
CustomerDimension.id
。它应该与引用的
Customer.id
相同<代码>客户是生成id@Cheetah然后您可以将FK定义为PK,但这似乎是一个奇怪的用例。编辑了答案。这是一个单独的表,因为
CustomerDimension
记录很大,并不总是必需的。它实际上是一对一的映射,因此客户只有一个维度。但它是双向的,但我不明白的是,
CustomerDimension.id
上没有注释……这个值是如何填充的?是什么让你认为它是一个?您需要CustomerDimension.ID上的典型ID注释。我把它们漏掉了,因为你漏掉了。请参见编辑。
public class CustomerDimension {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    @Column(name = "detail")
    private String detail;

    @ManyToOne
    Customer customer;

}