使用Hibernate注释具有外部id的类的正确方法是什么?

时间:2017-09-06 23:04:08

标签: java mysql hibernate annotations

我正在努力将现有项目转换为使用Hibernate。我有一个这样的课:

@Entity
@Table(name = "user")
public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "user_id")
   private Long userId;

   @Column(name = "group_id_user")
   private Long groupId;

   @Column(name = "name")
   private String name;

   ...
   // getters and setters....   
}

和这样的课程:

@Entity
@Table(name = "group")
public class Group {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "group_id")
   private Long groupId;

   @Column(name="group_name")
   private String groupName;

   ...
   // getters and setters....   
}

"group_id_user"表中名为user的列应该是"group_id"表中名为group的列的外键。

没关系,或者"正确"如上所示构建类,或者它们的结构如下所示,以确保数据库中存在外键?

@Entity
@Table(name = "user")
public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "user_id")
   private Long userId;

   @ManyToOne
   @JoinColumn(name = "group_id_user")
   private Group group;

   @Column(name = "name")
   private String name;

   ...
   // getters and setters....   
}

@Entity
@Table(name = "group")
public class Group {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "group_id")
   private Long groupId;

   @Column(name="group_name")
   private String groupName;

   ...
   // getters and setters....   
}

我尝试过使用这两种格式,但两种方式都有问题。当我使用第一种格式时,我在创建查询时遇到了连接的HQL语法问题。当我尝试第二种格式时,我遇到的问题是,在没有User的情况下从数据库中仅提取Group,或者从json对象添加新的User时,有groupId而不是Group对象。所以在我再花时间在两种格式之间来回切换之前我肯定知道哪种方式我应该使用最适合行业标准的注释?

1 个答案:

答案 0 :(得分:0)

如果您也可以更改列的名称,我会尝试这样的事情:

var schema = JSON.parse(postman.getEnvironmentVariable('myEnvVarName'));
let valid = tv4.validate(pm.response.json(), schema, false, true);

另外,如果可以,我可以将表的名称更改为复数。

我也使用能帮助我很多的东西。我有一个名为"可识别"它只有id,它看起来像这样:

@Entity
@Table(name = "users")
public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY) //This means you will need the database to generate the ids, if you are using Oracle this won't work. You would need to use SEQUENCE.
   private Long id;

   @ManyToOne
   @JoinColumn(name = "group_id") //There will be no need to specify the join column if you use group_id.
   private Group group;

   @Column(name = "name")
   private String name;

   ...
   // getters and setters....   
}

@Entity
@Table(name = "groups")
public class Group {

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

   @Column(name="group_name")
   private String groupName;

   ...
   // getters and setters....   
}

有了这个,您可以使用ID轻松扩展所有类:

@MappedSuperclass
public class Identifiable implements Serializable {

    private static final long serialVersionUID = -9027542469937539859L;

    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Long id;

    public Long getId() {
        return id;
    }

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

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Identifiable other = (Identifiable) obj;
        if (id == null) {
            return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

}

但是,如果您无法更改名称,请告诉我们您正在投掷的痕迹所遇到的问题,我们可能会提供帮助。

谢谢!