我正在尝试在具有ManyToOne关系的实体上在Hibernate中创建映射。我正在尝试这个:
CampaignItemSlot类:
package models;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "campaign_item_slots")
public class CampaignItemSlot {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@JoinColumn(name = "advert_slot_id")
@ManyToOne
private AdvertSlot advertSlot;
private boolean active;
private Timestamp date_created;
private Timestamp date_updated;
public CampaignItemSlot() {
super();
// TODO Auto-generated constructor stub
}
}
但是我在日志文件中得到了这个:
Caused by: org.hibernate.HibernateException: Missing column: advertSlot_id in text_advertising.campaign_item_slots
这是我的表SQL:
CREATE TABLE IF NOT EXISTS `text_advertising`.`campaign_item_slots` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`campaign_item_id` BIGINT NOT NULL,
`advert_slot_id` BIGINT NOT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`date_created` DATETIME NOT NULL,
`date_updated` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_campaignitems_1_idx` (`campaign_item_id` ASC),
INDEX `fk_campaignitems_2_idx` (`advert_slot_id` ASC),
CONSTRAINT `fk_campaign_item_slots_1`
FOREIGN KEY (`campaign_item_id`)
REFERENCES `text_advertising`.`campaignitems` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_campaign_item_slots_2`
FOREIGN KEY (`advert_slot_id`)
REFERENCES `text_advertising`.`advert_slots` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
AdvertSlot类:
package models;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "advert_slots")
public class AdvertSlot {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@ManyToOne
private Publication publication;
private String name;
private String description;
private boolean active;
private Timestamp date_created;
private Timestamp date_updated;
public AdvertSlot() {
super();
// TODO Auto-generated constructor stub
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Publication getPublication() {
return publication;
}
public void setPublication(Publication publication) {
this.publication = publication;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Timestamp getDate_created() {
return date_created;
}
public void setDate_created(Timestamp date_created) {
this.date_created = date_created;
}
public Timestamp getDate_updated() {
return date_updated;
}
public void setDate_updated(Timestamp date_updated) {
this.date_updated = date_updated;
}
}
不知怎的,Hibernate没有看到我的advert_slot_id,请帮忙吗?
答案 0 :(得分:1)
这里的答案是通过扩展org.hibernate.cfg.DefaultNamingStrategy然后通过hibernate config引用它来创建自定义命名策略:hibernate.ejb.naming_strategy
以下是一个例子:
@Override
public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {
String changed = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, propertyName) + "_id";
return changed;
}

答案 1 :(得分:1)
它将起作用。尝试这种方式:
第一张桌子:
words = zenPython.split()
for i in [',', '.', '-', '*', '!']:
words = [x.strip(i) for x in words] #list of words with ", . - * !" characters removed
第二张表:
@Table(name = "buyer_city")
public class BuyerCity{
//other fields
.....
@OneToMany(mappedBy="buyerCity", fetch = FetchType.LAZY)
@JsonManagedReference
private Set<BuyerCityMapping> buyerCityMapping = new HashSet<>();
}