- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有问题similar但也不完全是。在这些情况下(以及在 JPAContainer 示例中),ManyToOne 关系的实体部分只有一个键。就我而言,它是一个嵌入式 id。
我的代码基于Address Book example .
以下是三个实体:
@Entity
@Table(name = "tutorial")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Tutorial.findAll", query = "SELECT t FROM Tutorial t"),
@NamedQuery(name = "Tutorial.findById", query = "SELECT t FROM Tutorial t WHERE t.tutorialPK.id = :id"),
@NamedQuery(name = "Tutorial.findByTutorialTypeId", query = "SELECT t FROM Tutorial t WHERE t.tutorialPK.tutorialTypeId = :tutorialTypeId"),
@NamedQuery(name = "Tutorial.findByMessage", query = "SELECT t FROM Tutorial t WHERE t.message = :message")})
public class Tutorial implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected TutorialPK tutorialPK;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 245)
@Column(name = "message")
private String message;
@JoinColumn(name = "tutorial_type_id", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private TutorialType tutorialType;
public Tutorial() {
}
public Tutorial(TutorialPK tutorialPK) {
this.tutorialPK = tutorialPK;
}
public Tutorial(TutorialPK tutorialPK, String message) {
this.tutorialPK = tutorialPK;
this.message = message;
}
public Tutorial(int id, int tutorialTypeId) {
this.tutorialPK = new TutorialPK(id, tutorialTypeId);
}
public TutorialPK getTutorialPK() {
return tutorialPK;
}
public void setTutorialPK(TutorialPK tutorialPK) {
this.tutorialPK = tutorialPK;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public TutorialType getTutorialType() {
return tutorialType;
}
public void setTutorialType(TutorialType tutorialType) {
this.tutorialType = tutorialType;
}
@Override
public int hashCode() {
int hash = 0;
hash += (tutorialPK != null ? tutorialPK.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Tutorial)) {
return false;
}
Tutorial other = (Tutorial) object;
if ((this.tutorialPK == null && other.tutorialPK != null) || (this.tutorialPK != null && !this.tutorialPK.equals(other.tutorialPK))) {
return false;
}
return true;
}
@Override
public String toString() {
return "games.jwrestling.server.game.db.persistence.Tutorial[ tutorialPK=" + tutorialPK + " ]";
}
}
还有EmbeddedId类:
@Embeddable
public class TutorialPK implements Serializable {
@Basic(optional = false)
@NotNull
@Column(name = "id")
private int id;
@Basic(optional = false)
@NotNull
@Column(name = "tutorial_type_id")
private int tutorialTypeId;
public TutorialPK() {
}
public TutorialPK(int id, int tutorialTypeId) {
this.id = id;
this.tutorialTypeId = tutorialTypeId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getTutorialTypeId() {
return tutorialTypeId;
}
public void setTutorialTypeId(int tutorialTypeId) {
this.tutorialTypeId = tutorialTypeId;
}
@Override
public int hashCode() {
int hash = 0;
hash += (int) id;
hash += (int) tutorialTypeId;
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof TutorialPK)) {
return false;
}
TutorialPK other = (TutorialPK) object;
if (this.id != other.id) {
return false;
}
if (this.tutorialTypeId != other.tutorialTypeId) {
return false;
}
return true;
}
@Override
public String toString() {
return "games.jwrestling.server.game.db.persistence.TutorialPK[ id=" + id + ", tutorialTypeId=" + tutorialTypeId + " ]";
}
}
还有一个:
@Entity
@Table(name = "tutorial_type")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "TutorialType.findAll", query = "SELECT t FROM TutorialType t"),
@NamedQuery(name = "TutorialType.findById", query = "SELECT t FROM TutorialType t WHERE t.id = :id"),
@NamedQuery(name = "TutorialType.findByType", query = "SELECT t FROM TutorialType t WHERE t.type = :type"),
@NamedQuery(name = "TutorialType.findByDescription", query = "SELECT t FROM TutorialType t WHERE t.description = :description")})
public class TutorialType implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@GeneratedValue(strategy = GenerationType.TABLE, generator = "TutorialTypeGen")
@TableGenerator(name = "TutorialTypeGen", table = "jwrestling_id",
pkColumnName = "tablename",
valueColumnName = "last_id",
pkColumnValue = "tutorial_type",
allocationSize = 1,
initialValue = 1)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "type")
private String type;
@Size(max = 245)
@Column(name = "description")
private String description;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tutorialType")
private List<Tutorial> tutorialList;
public TutorialType() {
}
public TutorialType(Integer id) {
this.id = id;
}
public TutorialType(Integer id, String type) {
this.id = id;
this.type = type;
}
public TutorialType(String type, String desc) {
this.type = type;
this.description = desc;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlTransient
public List<Tutorial> getTutorialList() {
return tutorialList;
}
public void setTutorialList(List<Tutorial> tutorialList) {
this.tutorialList = tutorialList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof TutorialType)) {
return false;
}
TutorialType other = (TutorialType) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "games.jwrestling.server.game.db.persistence.TutorialType[ id=" + id + " ]";
}
}
我设置了表单,当我编辑项目时它工作正常,但创建时出现错误,因为 TutorialPK 为空:
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'tutorial_type_id' cannot be null
以下是相关的 Vaadin 代码:
public final class TutorialEditor extends Window implements Button.ClickListener,
FormFieldFactory {
private final Item tutorialItem;
private final Form editorForm;
private final Button saveButton;
private final Button cancelButton;
public TutorialEditor(Item tutorialItem) {
this.tutorialItem = tutorialItem;
editorForm = new Form();
editorForm.setFormFieldFactory(this);
editorForm.setBuffered(true);
editorForm.setImmediate(true);
editorForm.setItemDataSource(tutorialItem, Arrays.asList("message",
"tutorialType"));
saveButton = new Button("Save", this);
cancelButton = new Button("Cancel", this);
editorForm.getFooter().addComponent(saveButton);
editorForm.getFooter().addComponent(cancelButton);
setSizeUndefined();
setContent(editorForm);
setCaption("New Tutorial");
}
@Override
public void buttonClick(Button.ClickEvent event) {
if (event.getButton() == saveButton) {
editorForm.commit();
fireEvent(new EditorSavedEvent(this, tutorialItem));
} else if (event.getButton() == cancelButton) {
editorForm.discard();
}
close();
}
@Override
public Field<?> createField(Item item, Object propertyId, Component uiContext) {
Field field = DefaultFieldFactory.get().createField(item, propertyId,
uiContext);
if ("tutorialType".equals(propertyId)) {
field = new TutorialTypeSelector();
} else if (field instanceof TextField) {
((TextField) field).setNullRepresentation("");
}
field.addValidator(new BeanValidator(Tutorial.class, propertyId
.toString()));
return field;
}
public void addListener(EditorSavedListener listener) {
try {
Method method = EditorSavedListener.class.getDeclaredMethod(
"editorSaved", new Class[]{EditorSavedEvent.class});
addListener(EditorSavedEvent.class, listener, method);
} catch (final java.lang.NoSuchMethodException e) {
// This should never happen
throw new java.lang.RuntimeException(
"Internal error, editor saved method not found");
}
}
public void removeListener(EditorSavedListener listener) {
removeListener(EditorSavedEvent.class, listener);
}
public static class EditorSavedEvent extends Component.Event {
private final Item savedItem;
public EditorSavedEvent(Component source, Item savedItem) {
super(source);
this.savedItem = savedItem;
}
public Item getSavedItem() {
return savedItem;
}
}
public interface EditorSavedListener extends Serializable {
public void editorSaved(EditorSavedEvent event);
}
还有一个:
class TutorialTypeSelector extends CustomField<TutorialType> {
private final JPAContainer<TutorialType> container;
private final ComboBox type = new ComboBox();
public TutorialTypeSelector() {
container = JPAContainerFactory.make(TutorialType.class,
"JWPUJNDI");
setCaption("Type");
type.setContainerDataSource(container);
type.setItemCaptionPropertyId("type");
type.addListener(new Property.ValueChangeListener() {
@Override
public void valueChange(
com.vaadin.data.Property.ValueChangeEvent event) {
/*
* Modify the actual value of the custom field.
*/
if (type.getValue() == null) {
setValue(null, false);
} else {
TutorialType entity = container
.getItem(type.getValue()).getEntity();
setValue(entity, false);
}
}
});
}
@Override
protected Component initContent() {
CssLayout cssLayout = new CssLayout();
cssLayout.addComponent(type);
return cssLayout;
}
@Override
public void setPropertyDataSource(Property newDataSource) {
super.setPropertyDataSource(newDataSource);
setTutorialType((TutorialType) newDataSource.getValue());
}
@Override
public void setValue(TutorialType newValue) throws ReadOnlyException,
Converter.ConversionException {
super.setValue(newValue);
setTutorialType(newValue);
}
private void setTutorialType(TutorialType type) {
this.type.setValue(type != null ? type.getId() : null);
}
@Override
public Class<? extends TutorialType> getType() {
return TutorialType.class;
}
}
知道如何填充此字段吗?
更新:
使用@MapsId后出错
Exception [EclipseLink-46] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: There should be one non-read-only mapping defined for the primary key field [tutorial.tutorial_type_id].
Descriptor: RelationalDescriptor(games.jwrestling.server.game.db.persistence.Tutorial --> [DatabaseTable(tutorial)])
最佳答案
两种方式。如果使用 JPA 1.0,您将需要从引用的tutorialType 中提取值并将其手动添加到tutorial.tutorialPK.tutorialTypeId。您没有包含tutorialType实体,但如果生成了它的ID值,您可能需要在分配值之前保留它并刷新。
如果使用 JPA 2.0,您可以指定 @MapsId实体中的注释,允许 JPA 从tutorial.tutorialType 引用中设置 tuturial.tutorialPK.tutorialTypeId 值:
public class Tutorial implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected TutorialPK tutorialPK;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 245)
@Column(name = "message")
private String message;
@MapsId("tutorialTypeId")
@ManyToOne(optional = false)
private TutorialType tutorialType;
创建教程后,您将无法更改与教程关联的教程类型 - 唯一的选择是删除现有教程并使用新值创建一个新教程类型。
关于java - Vaadin JPAContainer : ManytoOne relation with EmbeddedID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36460071/
我有一个表,其中包含一个序列和两个外键的复合主键 我能够持久化我的实体类,但它没有按照顺序生成。由一个序列和两个外键组成的复合主键的表,maven中的hbm2java给出了以下实体 这是主要实体 pa
我有一个 hibernate 映射问题。我有以下两个数据库表(我不允许更改数据库): LOCATIONS { ID, -- PK NAME } LOCATION_GROUPS { L
我知道Hibernate中@EmbeddedId注解的复合主键至少由嵌入主键对象的所有对象变量的组合组成。 但是主键对象本身的引用是否也属于复合主键,因此它是由嵌入主键对象的引用值和对象变量构建的?
我对我的表格/实体有疑问。 我的 SQL 表如下所示: 表 1: IdTable1(pk) 属性... 表 2: IdTable2(pk) 属性... 表3: IdTable1(pk) IdTable
这是我的问题 IdComposite 类: @Embeddable public class IdComposite implements Serializable{ @Column(name =
我正在尝试使用引用另一个表的复合主键。我看到了很多主题,但没有答案适用于我的情况。 在我的数据库中,列是由未添加 owner_id 的外键正确创建的。因此,即使员工不存在,我也可以添加新记录。我使用
我收到了一个 Eclipse 错误:@EmbeddedId . 这是实体: @Entity @Table(name = "PERSON") public class Person implements
所以我这里有一个看起来像这样的图表, 可以在此 Answer Here 中找到. +---------------+ +-------------------+ | PRODUCTS
当我尝试从 JPA 存储库获取实体列表时,总是会遇到这样的异常 org.springframework.orm.jpa.JpaSystemException: No default construct
我正在尝试在一个类之间创建一对一的关系,我们将其称为“第一”,另一个类将其称为“第二”。 如果 Second 类使用 @EmbeddedId 作为其主键,它不会允许我这样做,我该如何解决这个问题? 基
我正在使用 hibernate,并且我已经使用 hibernate 在 java 端创建了实体。我的一个数据库表有多个列作为主键,hibernate 使用 @EmbeddedId 来处理这个问题。 我
我有一个主类和一个辅助类,用于存储保存时的错误。错误可以有多种类型,主键是错误的主要类别和类型。这是我的类的映射: 我的初级类(class): @Entity @Table(name = "foo")
我有一个具有复合主键的@Entity。因此,我创建了一个包含 PK 字段的 @EmbeddedId。 问题:是直接通过id.*访问这些字段更好,还是应该在父类中创建getter/setter? 例子:
我有一个包含三个字段 A、B、C 的 @Entity,其中 A 和 B 充当复合主键。我创建了一个包含 A 和 B 的 @EmbeddedId 类。为了减轻定义 getter 和 setter 的负担
在使用 hibernate 和 JPA 的 Spring MVC 应用程序中,我尝试为其基础数据表具有两列主键的实体设置映射。 如何更改下面的代码以使其正常工作? 我创建了一个名为 conceptPK
我有我的实体: @Entity @Table(name="performances") @AssociationOverrides({ @AssociationOverride(name="i
我有点麻烦。我正在使用 JPA Criteria 进行动态选择(使用标准作为 where 子句具有可选变量...)但我的实体之一有一个 EmbeddedId ,其中包含其中的用户列并且需要检查用户 I
我想使用条件进行以下查询。我有一个定义了 EmbeddedId 的 Entity: @Entity @Table(name="TB_INTERFASES") public class Inter
我的 @MapsId 注释和 @EmbeddedId 存在问题。在 Hibernate 中运行代码时,我得到: Caused by: org.hibernate.PropertyAccessExcep
我有以下实体: @Entity(name = "game_users") public class GameUser { private GameUsersPK primaryKey;
我是一名优秀的程序员,十分优秀!