如何通过非ID字段建立多对多关联。

huangapple go评论80阅读模式
英文:

How to link many to many not by ID

问题

2个实体:House(房屋)和City(城市),以及数据库截图。我意识到连接不正确,因为当用户发出请求时,他需要输入一个城市的编号,而不是名称,这是不正确的。如何正确地连接,以便在发生请求时,将城市的名称添加到House并与City建立关联?

City实体类:

@Data
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Table(name = "city", schema = "public")
public class City {

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

    @OneToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
            CascadeType.REFRESH
    }, mappedBy = "city")
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @JsonIgnore
    private Set<House> house;

    @Column(name = "id_region", nullable = false)
    private Integer id_region;
    @Column(name = "name", nullable = false)
    private String name;
}

House实体类:

@Data
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Table(name = "house", schema = "public")
public class House {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "city_id")
    private City city;

    @OneToMany(mappedBy = "house")
    private Set<Contract> contract;

    @Column(name = "id_landlord", nullable = false)
    private Long id_landlord;
    @Column(name = "outside", nullable = false)
    private String outside;
    @Column(name = "rooms", nullable = false)
    private Integer rooms;
    @Column(name = "price", nullable = false)
    private Double price;
    @Column(name = "description", nullable = false)
    private String description;
    @Enumerated(value = EnumType.STRING)
    @Column(name = "status")
    private Status status;
}

(截图已略去)

英文:

2 entities House and City and a screenshot of the database. Now I realized that I didn't link it quite correctly, because when a user makes a request, he needs to drive in a number, not a name, of a city, which is not correct. How can I link correctly so that when a request occurs, the name of the city is added to the House and has links with the City?

City

@Data
@Entity
@JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;, &quot;handler&quot;})
@Table(name = &quot;city&quot;, schema = &quot;public&quot;)
public class City {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = &quot;id&quot;)
private Long id;

@OneToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
        CascadeType.REFRESH
}, mappedBy = &quot;city&quot;)
@ToString.Exclude
@EqualsAndHashCode.Exclude
@JsonIgnore
private Set&lt;House&gt; house;

@Column(name = &quot;id_region&quot;, nullable = false)
private Integer id_region;
@Column(name = &quot;name&quot;, nullable = false)
private String name;
}

House

@Data
@Entity
@JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;, &quot;handler&quot;})
@Table (name = &quot;house&quot;, schema = &quot;public&quot;)
public class House {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = &quot;id&quot;)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = &quot;city_id&quot;)
    private City city;

    @OneToMany(mappedBy = &quot;house&quot;)
    private Set&lt;Contract&gt; contract;

    @Column(name = &quot;id_landlord&quot;, nullable = false)
    private Long id_landlord;
    @Column(name = &quot;outside&quot;, nullable = false)
    private String outside;
    @Column(name = &quot;rooms&quot;, nullable = false)
    private Integer rooms;
    @Column(name = &quot;price&quot;, nullable = false)
    private Double price;
    @Column(name = &quot;description&quot;, nullable = false)
    private String description;
    @Enumerated(value = EnumType.STRING)
    @Column(name = &quot;status&quot;)
    private Status status;
}

如何通过非ID字段建立多对多关联。

答案1

得分: 0

你需要通过“id”在数据库中链接实体(有少数例外情况)。在代码中,例如创建“House”的代码中,“id”是从何处获取的,现在我们将考虑这一点。

实际上,通常用户不会输入“id”,因为这很不方便。但是,在文本框中指定城市的名称也不够用户友好,他们不会这样做。例如,如果用户输入错误拼写会发生什么?

重要的是,用户不能指定任意城市。如果房屋中存储了与城市的链接(直接或通过中间表),那么用户只能从现有的城市中进行选择。

实际上,这意味着系统会向用户提供城市的选择。可以是下拉列表,也可以是逐步搜索并确认选择,或者按条件搜索,同样需要确认选择。重要的是,用户在界面上看到的是城市的确切名称,并且这个城市基本上是可以选择的。在进行选择时,程序会使用所选城市的“id”来创建“House”。

因此,具体的实现取决于您在用户界面中使用的内容。在数据库、服务甚至MVC控制器中,都需要使用“id”。

英文:

You need to link entities in the database (with rare exceptions) by id. Where the id comes from in the code that, say, creates House is another question, and now we will consider it.

Indeed, usually the user does not enter an id, as this is inconvenient. But specifying the name of the city in the text box is also not user-friendly, and they don't do that. For example, what happens if a user makes a typo?

It is important that the user does not specify an arbitrary city. If a link to a city is stored in the House (directly or through an intermediate table - it doesn't matter), then the user chooses from the existing cities.

In practice, this means that the system gives the user a choice of a city. It can be either a drop-down list, or an incremental search with confirmation of the selection, or a search by criteria, again, with confirmation of the selection. The important thing here is that in the UI the user sees the exact name of the city, then that this city, in principle, is available for selection. At the very same choice, the program uses the id of the selected city to create a House.

Therefore, the specific implementation depends on what you use in the UI. In a database, service and even MVC controller, you need to use id.

huangapple
  • 本文由 发表于 2020年9月28日 21:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64103362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定