JPA 不会通过包装对象将 Id 添加到数据库
使用 JPA 我有一个数据配置,它可以毫无问题地添加到数据库中:
@Entity @Table(name = "dataConfigurations", uniqueConstraints = {@UniqueConstraint(columnNames = {"name"})})
@NamedQueries({@NamedQuery(name = "DataConfigurations.names", query = "SELECT d.name FROM DataConfiguration d"), @NamedQuery(name = "DataConfiguration.findByName", query = "SELECT d FROM DataConfiguration d WHERE d.name = :name")})
public class DataConfiguration implements Serializable
{
@Transient
private static final long serialVersionUID = 1L;
@Id //@GeneratedValue(strategy = GenerationType.)
//@TableGenerator(name = "dataConfigurationSequence", table = "Sequences", pkColumnName = "NameSequences", valueColumnName = "valueSequence", pkColumnValue = "dataConfigurationId")
private long id;
@OneToMany(mappedBy = "dataConfiguration", cascade = CascadeType.ALL)
private List<Content> contents; // "Data" is a reserved SQL word.
@ManyToMany(mappedBy = "dataConfigurations", cascade = CascadeType.ALL)
private List<LabelType> labelTypes;
private String name;
@OneToMany(mappedBy = "dataConfiguration", cascade = CascadeType.ALL
private List<Template> templates;
此数据配置包含一个内容列表(一对多关系), 其他映射(模板和 labelTypes 现在并不重要,但稍后在我的程序中,它们可能会给我带来与内容相同的问题)
@Entity @Table(name = "contents", uniqueConstraints = {@UniqueConstraint(columnNames = {"dataConfigurationId", "displayOrder"})})
public class Content implements Serializable
{
@Transient
private static transient final long serialVersionUID = 1L;
@Id //@GeneratedValue(strategy = GenerationType.IDENTITY)
//@TableGenerator(name = "contentSequence", table = "Sequences", pkColumnName = "NameSequences", valueColumnName = "valueSequence", pkColumnValue = "contentId")
private long id;
private String content; // "Data" is a reserved word in traditional SQL.
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "dataConfigurationId")
private DataConfiguration dataConfiguration;
private int displayOrder;
private int editableFrom;
private String typeDiscriminator;
这是启动保存方法的控制器方法:
public String saveDataConfiguration()
{
dataConfiguration = new DataConfiguration(getNextId(), dataConfigurationName, contents);
dataConfigurationFacade.create(dataConfiguration);
return null;
}
创建方法是一部分AbstractFacade 的
public void create(T entity)
{
getEntityManager().persist(entity);
getEntityManager().flush();
}
当我执行该方法时,除了内容表中数据配置的 Id 之外的所有内容都会被保存。 (数据配置的外键不保存在内容中) 任何提示和/或技巧都会非常有帮助!
Using JPA I have a dataconfiguration, which is added to the database with no problems:
@Entity @Table(name = "dataConfigurations", uniqueConstraints = {@UniqueConstraint(columnNames = {"name"})})
@NamedQueries({@NamedQuery(name = "DataConfigurations.names", query = "SELECT d.name FROM DataConfiguration d"), @NamedQuery(name = "DataConfiguration.findByName", query = "SELECT d FROM DataConfiguration d WHERE d.name = :name")})
public class DataConfiguration implements Serializable
{
@Transient
private static final long serialVersionUID = 1L;
@Id //@GeneratedValue(strategy = GenerationType.)
//@TableGenerator(name = "dataConfigurationSequence", table = "Sequences", pkColumnName = "NameSequences", valueColumnName = "valueSequence", pkColumnValue = "dataConfigurationId")
private long id;
@OneToMany(mappedBy = "dataConfiguration", cascade = CascadeType.ALL)
private List<Content> contents; // "Data" is a reserved SQL word.
@ManyToMany(mappedBy = "dataConfigurations", cascade = CascadeType.ALL)
private List<LabelType> labelTypes;
private String name;
@OneToMany(mappedBy = "dataConfiguration", cascade = CascadeType.ALL
private List<Template> templates;
This Dataconfiguration contains a list of Contents (one to many relationship),
the other mappings (templates en labelTypes do not matter for now but will later on in my program, they will probably give me the same problem as I have with contents)
@Entity @Table(name = "contents", uniqueConstraints = {@UniqueConstraint(columnNames = {"dataConfigurationId", "displayOrder"})})
public class Content implements Serializable
{
@Transient
private static transient final long serialVersionUID = 1L;
@Id //@GeneratedValue(strategy = GenerationType.IDENTITY)
//@TableGenerator(name = "contentSequence", table = "Sequences", pkColumnName = "NameSequences", valueColumnName = "valueSequence", pkColumnValue = "contentId")
private long id;
private String content; // "Data" is a reserved word in traditional SQL.
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "dataConfigurationId")
private DataConfiguration dataConfiguration;
private int displayOrder;
private int editableFrom;
private String typeDiscriminator;
This is the controller method that initiates the save method:
public String saveDataConfiguration()
{
dataConfiguration = new DataConfiguration(getNextId(), dataConfigurationName, contents);
dataConfigurationFacade.create(dataConfiguration);
return null;
}
The create method is part of the AbstractFacade
public void create(T entity)
{
getEntityManager().persist(entity);
getEntityManager().flush();
}
When I execute the method everything except the Id of the dataconfiguration in the content table is saved.
(Foreign key of the dataconfiguration is not saved in content)
Any hints and/or tips would very helpful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能忘记初始化每个内容的
dataConfiguration
字段。这是映射到连接列的列,因此 Hibernate 将检查它以确定两个实体之间是否存在关联。You probably forgot to initialize the field
dataConfiguration
of every content. That's the one which is mapped to the join column, and it's thus the one that Hibernate inspects to determine if there is an association between the two entities or not.