我的情况是,我有一个实体VCenterDistributedVirtualPortgroup,它扩展了VCenterNetwork,并且这两个实体都在实体VCenterFolder中的OneToMany关系中。我收到以下错误:
引起:org.h2.jdbc.JdbcSQLException:列不允许NULL" NETWORK_TYPE&#34 ;; SQL语句: 插入folder_network(folder_type,folder_val,distributedVirtualPortgroups_type,distributedVirtualPortgroups_val)值(?,?,?,?)[23502-182]
VCenterNetwork:
@Entity
@Embeddable
@Table(name="network")
@DiscriminatorColumn(name="discriminator")
@DiscriminatorValue("Network")
public class VCenterNetwork
{
@Transient
private Network network;
@Transient
private static Map<MOR, VCenterNetwork> networkMap = new TreeMap<MOR, VCenterNetwork>();
@EmbeddedId
private MOR id;
public MOR getId() {return this.id;}
public void setId(MOR id) {this.id = id;}
...
}
VCenterDistributedVirtualPortgroup:
@Entity
@Table(name="distributedvirtualportgroup")
@DiscriminatorColumn(name="discriminator")
@DiscriminatorValue("DistributedVirtualPortgroup")
public class VCenterDistributedVirtualPortgroup extends VCenterNetwork
{
@Transient
private DistributedVirtualPortgroup distributedVirtualPortgroup;
@Transient
private static Map<MOR, VCenterDistributedVirtualPortgroup> distributedVirtualPortgroupMap = new TreeMap<MOR, VCenterDistributedVirtualPortgroup>();
...
}
VCenterFolder:
@Entity
@Table(name="folder")
public class VCenterFolder
{
@Transient
private Folder folder;
@Transient
private static Map<MOR, VCenterFolder> folderMap = new TreeMap<MOR, VCenterFolder>();
@EmbeddedId
private MOR id;
public MOR getId() {return this.id;}
public void setId(MOR id) {this.id = id;}
@Embedded
@OneToMany(cascade=CascadeType.ALL)
private List<VCenterNetwork> network = new ArrayList<VCenterNetwork>();
public List<VCenterNetwork> getNetwork() {return this.network;}
public void getvirtualNetwork(List<VCenterNetwork> network) {this.network = network;}
@Embedded
@OneToMany(cascade=CascadeType.ALL)
private List<VCenterDistributedVirtualPortgroup> distributedVirtualPortgroups = new ArrayList<VCenterDistributedVirtualPortgroup>();
public List<VCenterDistributedVirtualPortgroup> getDistributedVirtualPortgroups() {return this.distributedVirtualPortgroups;}
public void setDistributedVirtualPortgroups(List<VCenterDistributedVirtualPortgroup> distributedVirtualPortgroups){this.distributedVirtualPortgroups = distributedVirtualPortgroups;}
....
}
这会产生如下所示的连接表:
FOLDER_NETWORK
FOLDER_TYPE - VARCHAR(255) NOT NULL
FOLDER_VAL - VARCHAR(255) NOT NULL
NETWORK_TYPE - VARCHAR(255) NOT NULL
NETWORK_VAL - VARCHAR(255) NOT NULL
DISTRIBUTEDVIRTUALPORTGROUP_TYPE - VARCHAR(255) NOT NULL
DISTRIBUTEDVIRTUALPORTGROUP_TYPE - VARCHAR(255) NOT NULL
我是JPA的新手,但我的猜测是VCenterFolder-VCenterNetwork和VCenterFolder-VCenterDistributedVirtualPortgroup都需要连接表,但由于VCenterDistributedVirtualPortgroup扩展了VCenterNetwork,它构建了一个具有两种关系的连接表但是为每个关系使用不同的类型/值对。我假设一次只使用一对,另一对为空。这似乎是一个问题。在我看来,字段应该是可空的,或者两组type / val对应该合并为一个。
我认为这有一些方法,但我确定不知道它是什么。
答案 0 :(得分:2)
MOR碰巧有会员类型&amp; val,因此是连接表中的名称。
我通过在网络和distributedVirtualPortgroups字段中添加以下JoinTable来解决问题:
@JoinTable
(
name="FOLDER_NETWORK",
joinColumns= {@JoinColumn(name="TYPE", referencedColumnName="TYPE"), @JoinColumn(name="VAL", referencedColumnName="VAL")},
inverseJoinColumns= {@JoinColumn(name="NETWORK_TYPE", referencedColumnName="TYPE"), @JoinColumn(name="NETWORK_VAL", referencedColumnName="VAL")}
)