【问题标题】:HIbernate OneToMany FetchType.EAGER not returning valuesHIbernate OneToMany FetchType.EAGER 不返回值
【发布时间】:2014-06-01 20:37:09
【问题描述】:

我使用 Hibernate 3.5.6 和 MySQL 5.1。 我有 3 个类:项目、属性、属性值。它们以一对多的关系相互关联,我确实获得了给定项目的所有属性。但我没有得到给定属性的 AttributeValues。我看不到异常或错误,表中的数据看起来不错,这意味着数据的插入很顺利。

我对Attribute的AttributeValues映射的FetchMode尝试了不同的策略,但结果总是为空。

我错过了什么?

以下是课程:

@Entity
@Table(name = "Item")
public class Item implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String serialNumber;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "item", cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SELECT)    // don't remove this line
    @MapKey(name = "name")
    private Map<String, Attribute> attributes = new HashMap<String, Attribute>();

    protected Item() {
        super();
    }

    public Item(String serialNumber) {
        this();
        setSerialNumber(serialNumber);
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    protected void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    public Map<String, Attribute> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, Attribute> attributes) {
        this.attributes = attributes;
    }
}

@Entity
@Table(name = "Attribute", uniqueConstraints = {@UniqueConstraint(columnNames = {"item_serialNumber", "name"})})
public class Attribute implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne(cascade = CascadeType.ALL)
    private Item item;

    @Column(nullable = false)
    private String name;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "value", cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SELECT)
    @MapKey(name = "value")
    private Map<String, AttributeValue> values = new HashMap<String, AttributeValue>();

    protected Attribute() {
        super();
    }

    public Attribute(String name) {
        super();
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    protected void setId(Integer id) {
        this.id = id;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public String getName() {
        return name;
    }

    protected void setName(String newName) {
        this.name = newName;
    }

    public Map<String, AttributeValue> getValues() {
        return values;
    }

    public void setValues(Map<String, AttributeValue> values) {
        this.values = values;
    }

}

@Entity
@Table(name = "AttributeValue", uniqueConstraints = {@UniqueConstraint(columnNames = {"attribute_id", "value"})})
public class AttributeValue implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column
    private String value;

    @ManyToOne(cascade = CascadeType.ALL)
    private Attribute attribute;

    protected AttributeValue() {
        super();
    }

    public AttributeValue(String value) {
        this.value = value;
    }

    protected Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    protected void setValue(String value) {
        this.value = value;
    }

    /**
     * @return the attribute
     */
    public Attribute getAttribute() {
        return attribute;
    }

    /**
     * @param attribute the attribute to set
     */
    public void setAttribute(Attribute attribute) {
        this.attribute = attribute;
    }
}

下面是底层表的 SQL:

--
-- Table structure for table `Attribute`
--

CREATE TABLE `Attribute` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `item_serialNumber` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `item_serialNumber` (`item_serialNumber`,`name`),
  KEY `FK7839CA7C252F491C` (`item_serialNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `Item`
--

CREATE TABLE `Item` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `serialNumber` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `serialNumber` (`serialNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `AttributeValue`
--

CREATE TABLE `AttributeValue` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `value` varchar(255) DEFAULT NULL,
  `attribute_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `attribute_id` (`attribute_id`,`value`),
  KEY `FK4C1BA6C69CA0A39A` (`attribute_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `Attribute`
--
ALTER TABLE `Attribute`
  ADD CONSTRAINT `FK7839CA7C252F491C` FOREIGN KEY (`item_serialNumber`) REFERENCES `Item` (`serialNumber`);

--
-- Constraints for table `AttributeValue`
--
ALTER TABLE `AttributeValue`
  ADD CONSTRAINT `FK4C1BA6C69CA0A39A` FOREIGN KEY (`attribute_id`) REFERENCES `Attribute` (`id`);

【问题讨论】:

    标签: java mysql hibernate one-to-many


    【解决方案1】:

    您已经使用“值”而不是“属性”在属性中映射了values 映射。 @OneToMany 注释应指定如下:

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "attribute", cascade = CascadeType.ALL)
    

    【讨论】:

    • 谢谢,我不敢相信我错过了。谢谢。
    猜你喜欢
    • 2010-12-31
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2020-06-21
    • 2020-01-04
    • 2018-01-12
    相关资源
    最近更新 更多