问题是,当我运行应用程序时,hibernate在tahograph\u cards表中创建了一个列user\u id,而我不想在users表中创建一个列tahograph\u card\u id。这是一段一夫多妻的关系。
这是我的档案:
应用程序属性:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/getmydrivercard
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
用户:
package com.getmydrivercard.getmydrivercard.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@Entity
@Table(name = "users")
public class User {
private static final String EMPTY_STRING = "";
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)")
private UUID userId;
@NotEmpty
private String username;
@NotEmpty
private String password;
@NotEmpty
private String email;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Set<TahographCard> tahographCards;
private String comments;
protected User(){}
public User(String username, String password, String email){
setUsername(username);
setPassword(password);
setEmail(email);
setComments(EMPTY_STRING);
setTahographCards();
}
//getters and setters
}
塔霍卡:
package com.getmydrivercard.getmydrivercard.models;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.util.UUID;
@Entity
@Table(name = "tahograph_cards")
public class TahographCard {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)")
private UUID tahographCardId;
@NotEmpty
private Boolean isActive;
@NotEmpty
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId")
private User user;
protected TahographCard(){}
public TahographCard(User user){
setUser(user);
setActive(true);
}
//getters and setters
}
我尝试了mappedby和其他一些我在互联网上找到的东西,但仍然不起作用。先谢谢你。
1条答案
按热度按时间egdjgwm81#
这里的问题不是实体配置,而是数据库设计。不能将“一对多”关系Map为一侧的fk列。
例如,如果您有2个用户和4张卡(每个用户2张),通常情况下,它会如下所示:
与用户表上的tahographcardid相反是不可能的