我有一个数据库架构。表提供程序与provider_curr和provider_country有许多关系(必须还原从prov_curr到提供程序的箭头)
类提供者,
package entity;
import Currency.Country;
import Currency.Currency;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name = "provider", schema = "public", catalog = "testdb")
public class ProviderEntity implements Comparable<ProviderEntity> {
private String name;
private int id;
private Set<Currency> currencies = new HashSet<>();
private Set<Country> countries = new HashSet<>();
public ProviderEntity() {
}
public Set<Currency> getCurrencies() {
return currencies;
}
public void setCurrencies(Set<Currency> currencies) {
this.currencies = currencies;
}
public Set<Country> getCountries() {
return countries;
}
public void setCountries(Set<Country> countries) {
this.countries = countries;
}
@Id
@Column(name = "id", nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name", nullable = false, length = 45)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ProviderEntity(String name, Set<Currency> currencies, Set<Country> countries) {
this.name = name;
this.currencies = currencies;
this.countries = countries;
}
@Override
public String toString() {
return "ProviderEntity " + name + "\n Currencies: " + currencies.toString() + "\n" + "Countries" + countries.toString() + "\n";
}
public static TreeSet<ProviderEntity> sortByName(Set<ProviderEntity> values) {
Comparator<ProviderEntity> comp = (ProviderEntity o1, ProviderEntity o2) -> (o1.compareTo(o2));
TreeSet<ProviderEntity> result = new TreeSet<>(comp);
values.stream().forEach(providerEntity -> result.add(providerEntity));
System.out.println(result);
return result;
}
public void addCurrency(String currency) {
for (int i = 0; i < Currency.values().length; i++) {
if (Currency.values()[i].toString().equals(currency)) {
currencies.add(Currency.values()[i]);
}
}
}
public void addCountry(String country) {
for (int i = 0; i < Country.values().length; i++) {
if (Country.values()[i].toString().equals(country)) {
countries.add(Country.values()[i]);
}
}
}
public boolean containsCurrencsy(String currency) {
for (int i = 0; i < Currency.values().length; i++) {
if (currency.equals(Currency.values()[i].toString())) {
return currencies.contains(Currency.values()[i]);
}
}
return false;
}
public boolean containsCountry(String country) {
for (int i = 0; i < Country.values().length; i++) {
if (country.equals(Country.values()[i].toString())) {
return countries.contains(Country.values()[i]);
}
}
return false;
}
@Override
public int compareTo(ProviderEntity o) {
return (o.name.compareTo(this.name));
}
}
与国家/地区联系,
public enum Country {
AUSTRALIA,
AUSTRIA,
AZERBAIJAN,
ALBANIA,
ALGERIA,
ANGOLA,
ARGENTINA,
ARMENIA,
ARUBA,
BANGLADESH
}
和货币枚举
public enum Currency {
DOLLAR,
EURO,
MANAT,
LEK,
DINAR,
KWANZA,
PESO,
DRAM,
GUILDER,
TAKA
}
我坚持使用映射设置货币和设置国家/地区。我该如何映射它们?
我试着这样做,但它不起作用。
@ElementCollection(targetClass = Currency.class)
@JoinTable(name = "provider_curr", joinColumns = @JoinColumn(name = "id"))
@Column(name = "id", nullable = false)
@Enumerated(EnumType.STRING)
public Set<Currency> getCurrencies() {
return currencies;
}
public void setCurrencies(Set<Currency> currencies) {
this.currencies = currencies;
}
@ElementCollection(targetClass = Country.class)
@JoinTable(name = "provider_country", joinColumns = @JoinColumn(name = "id"))
@Column(name = "id", nullable = false)
@Enumerated(EnumType.STRING)
public Set<Country> getCountries() {
return countries;
}
public void setCountries(Set<Country> countries) {
this.countries = countries;
}
答案 0 :(得分:0)
也许我错了,我认为你的映射不正确。
我想如果您尝试使用@ElementCollection
和@JoinTable
执行此操作,则需要更改表格结构。
首先:您不再需要有provcountry
和provcurr
的交叉表。
第二:从表provider_country
和provider_currency
中删除列id
(您使用enum
代替entity
没有标识符)。这些表必须包含列provider_id
(FK到provider
表)。
它们应该如下所示:
// provider_country
-------------------------------------------------------
| provider_id: Long | country: String |
-------------------------------------------------------
| 1 | AUSTRALIA |
-------------------------------------------------------
| 2 | AUSTRALIA |
-------------------------------------------------------
| 1 | BANGLADESH |
-------------------------------------------------------
| 3 | AZERBAIJAN |
-------------------------------------------------------
// provider_currency
--------------------------------------------------------
| provider_id: Long | currency: String |
--------------------------------------------------------
| also can contain different currencies for different |
| providers like the provider_country table |
--------------------------------------------------------
映射应该是:
@CollectionOfElements(targetClass = Country.class)
@JoinTable(name = "provider_country", joinColumns = @JoinColumn(name = "provider_id"))
@Column(name = "country", nullable = false)
@Enumerated(EnumType.STRING)
public Collection<Country> getCountries() {
return countries;
}
@CollectionOfElements(targetClass = Currency.class)
@JoinTable(name = "provider_currency", joinColumns = @JoinColumn(name = "provider_id"))
@Column(name = "currency", nullable = false)
@Enumerated(EnumType.STRING)
public Collection<Currency> getCurrencies() {
return currencies;
}