简体   繁体   中英

JPA2.0 mapping a map of Map<Entity0,Entity1> how do I specify column names?

@ManyToMany
@JoinTable(schema = "cust_tables", joinColumns = @JoinColumn(name = "item_id"))
public Map<Attribute, Option> attributeValues;

Attribute is a property name, option is a valid value for that attribute.

EclipseLink currently generates the following DDL for the table:

CREATE TABLE `items_options` (
  `attributes_id` bigint(20) NOT NULL,
  `item_id` bigint(20) NOT NULL,
  `attributes_KEY` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`item_id`,`attributes_id`),
  KEY `FK_items_options_attributes_id` (`attributes_id`),
  KEY `FK_items_options_attributes_KEY` (`attributes_KEY`),
  CONSTRAINT `FK_items_options_attributes_id` FOREIGN KEY (`attributes_id`) REFERENCES `options` (`id`),
  CONSTRAINT `FK_items_options_attributes_KEY` FOREIGN KEY (`attributes_KEY`) REFERENCES `attributes` (`id`),
  CONSTRAINT `FK_items_options_item_id` FOREIGN KEY (`item_id`) REFERENCES `items` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

On the join table, I need to change the column names: 'attributes_id' and 'attributes_KEY'. How do I specify this?

To change the name of the column Attribute, try the annotation @MayKeyJoinColumn. Something like this:

@ManyToMany
@JoinTable(schema = "cust_tables", joinColumns = @JoinColumn(name = "item_id"))
@MapKeyJoinColumn(name="ATTRIBUTE")
public Map<Attribute, Option> attributeValues;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM