스프링 부트 프로젝트에 다 대다 관계가 있습니다. 함께 매핑되는 프로젝트의 두 엔티티 선언으로 모든 것이 정상입니다.Spring에 초기 데이터를 삽입하는 방법 Many to Many Relationshionsip
그러나 처음 테이블의 일부 초기 데이터를 포함하도록 응용 프로그램을 실행할 때 데이터를 테이블에 삽입하려고합니다. 이 일을하는 데 문제가 있습니다. 나는 관계를 제거 할 때이 잘 작동
다음이다 나는 JPA 주석을 사용하여 엔티티 선언 방법 :
가입자 엔티티
@Entity
@Table(name="subscribers")
public class Subscriber {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long subscriber_id;
@Column(name= "firstname")
@Size(min = 3, max = 20, message = "Please enter a valid first name")
@NotEmpty(message = "First Name is required.")
public String firstname;
@Column(name= "lastname")
@Size(min = 3, max = 20, message = "Please enter a valid first name")
@NotEmpty(message = "Last Name is required.")
public String lastname;
private Set<Cluster> clusters;
@Pattern(
regexp = "^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$",
message = "Invalid Email address. Enter Correct Email Address"
)
@Column(name= "emailaddress")
@NotEmpty(message = "Email is required.")
public String email;
public Subscriber()
{
super();
}
public Subscriber(String firstname, String lastname, String email, Set<Cluster> clusters)
{
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
public long getSubscriberId()
{
return subscriber_id;
}
public void setSubscriberId(long subscriber_id)
{
this.subscriber_id = subscriber_id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname()
{
return lastname;
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
@ManyToMany(mappedBy = "subscribers")
public Set<Cluster> getClusters()
{
return clusters;
}
public void setClusters (Set<Cluster> clusters)
{
this.clusters = clusters;
}
클러스터 엔티티
@Entity
public class Cluster {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long cluster_id;
@Column(name= "clustername")
@NotNull
public String clusterName;
@Column(name= "location")
public String location;
@Column(name= "apiaddress")
public String apiAddress;
@Column(name= "contact")
@NotNull
public String contact;
public Date creationDate;
public Set<Subscriber> subscribers;
public Cluster()
{
}
public Cluster(String clusterName, String location, String apiAddress, String contact, Set<Subscriber> subscribers)
{
super();
this.clusterName = clusterName;
this.location = location;
this.apiAddress = apiAddress;
this.contact = contact;
}
public long getClusterId()
{
return cluster_id;
}
public void setClusterId(long cluster_id)
{
this.cluster_id = cluster_id;
}
public String getClusterName()
{
return clusterName;
}
public void setClusterName (String clusterName)
{
this.clusterName = clusterName;
}
public String getLocation()
{
return location;
}
public void setLocation (String location)
{
this.location = location;
}
public String getApiAddress()
{
return apiAddress;
}
public void setApiAddress (String apiAddress)
{
this.apiAddress = apiAddress;
}
public String getContact()
{
return contact;
}
public void setContact (String contact)
{
this.contact = contact;
}
public Date getCreationDate()
{
return creationDate;
}
public void setCreationDate(Date creationDate)
{
this.creationDate = creationDate;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="cluster_subscriber", joinColumns = @JoinColumn(name="cluster_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "subscriber_id", referencedColumnName = "id"))
public Set<Subscriber> getSubscribers()
{
return subscribers;
}
public void setSubscribers(Set<Subscriber> subscribers)
{
this.subscribers =subscribers;
}
}
을 진입 점 인 데이터를 추가하는 데 사용되는 코드 i 봄 부팅 응용 프로그램 및 I는 오류
@SpringBootApplication
public class AiAdminApplication {
public static void main(String[] args) {
SpringApplication.run(AiAdminApplication.class, args);
/*ApplicationContext ctx = SpringApplication.run(AiAdminApplication.class, args);
String [] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String name: beanNames){
System.out.println(name);
}*/
}
@Component
public class DatabaseLoader implements CommandLineRunner {
private final SubscriberRepository subscriberRepository;
private final ClusterRepository clusterRepository;
@Autowired
public DatabaseLoader(SubscriberRepository subscriberRepository, ClusterRepository clusterRepository) {
this.subscriberRepository = subscriberRepository;
this.clusterRepository = clusterRepository;
}
public void run (String...strings) throws Exception {
this.subscriberRepository.save(new Subscriber("Olalekan Samuel", "Ogunleye", "[email protected]", 1));
this.subscriberRepository.save(new Subscriber("Pieter", "Erasmus", "[email protected]", 2));
this.subscriberRepository.save(new Subscriber("Nico", "Boss", "[email protected]", 3));
this.subscriberRepository.save(new Subscriber("Felix Parfait", "Tandem", "[email protected]", 4));
this.subscriberRepository.save(new Subscriber("Nkosinathi", "Nkosinatthi", "[email protected]", 5));
this.subscriberRepository.save(new Subscriber("Balleng", "Balleng", "[email protected]", 6));
this.subscriberRepository.save(new Subscriber("Nicolas", "Immelman", "[email protected]", 7));
this.subscriberRepository.save(new Subscriber("Nicolo", "Carzavallian", "[email protected]", 8));
this.subscriberRepository.save(new Subscriber("Shaun", "chang", "[email protected]", 9));
this.subscriberRepository.save(new Subscriber("Andre", "Immelman", "[email protected]", 10));
this.subscriberRepository.save(new Subscriber("Michel", "Immelman", "[email protected]", 11));
this.clusterRepository.save(new Cluster("Aws-eu-west-1", "Ireland", "123.98.45", "Olalekan Samuel", 1));
this.clusterRepository.save(new Cluster("Azure", "United State", "123.98.45", "Pieter Erasmus", 1));
this.clusterRepository.save(new Cluster("Google Cloud", "United State", "123.98.45", "Nico Boss", 1));
this.clusterRepository.save(new Cluster("herekou", "United Kingdom", "123.98.45", "Felix Parfait", 2));
this.clusterRepository.save(new Cluster("IBM", "United State", "123.98.45", "Nkosinathi Nkosinathi", 4));
this.clusterRepository.save(new Cluster("herekou", "United Kingdom", "123.98.45", "Balleng Immelman", 5));
this.clusterRepository.save(new Cluster("Aws-eu-west-1", "Ireland", "123.98.45", "Nicolas Immelman", 6));
this.clusterRepository.save(new Cluster("Azure", "United State", "123.98.45", "Nicolo Carzavallian", 6));
this.clusterRepository.save(new Cluster("Google Cloud", "United State", "123.98.45", "Shaun Chang", 5));
this.clusterRepository.save(new Cluster("herekou", "United Kingdom", "123.98.45", "Andre Immelman", 7));
this.clusterRepository.save(new Cluster("IBM", "United State", "123.98.45", "Michel Immelman"));
}
}
}
제가 적용 할 수없는 오류가 발생하고있어 무엇입니까 곳이다,하지만 난 매핑을 제거하고 ID가에 추가하면 모든 것이 괜찮 N 끝.
입력 응용 프로그램에 데이터를 삽입하는 방법에 대한 도움을 주시면 감사하겠습니다. 사전
생성자가 Set을 필요로하고 int 매개 변수를 전달하면 무시하겠습니까? – Zeromus
그 점을 지적 해 주셔서 감사합니다. 당신은 어떤 생성자가 Set을 기대하는지 말해 주시겠습니까? – olammy
다시 클러스터와 구독자 ... 모두 컴파일 할 수 없습니다. – Zeromus