2017-12-12 11 views
0

私はSpringブートプロジェクトで多対多の関係を持っています。一緒にマッピングされたプロジェクトの2つのエンティティの宣言によって、すべてがOKです。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であるデータを追加するために使用されるコード春ブートアプリケーションと、私はエラー

@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個終わり。

エントリーアプリケーションにデータを挿入する方法については、ご了承ください。

ありがとうございます。

+0

コンストラクタはSetを必要とし、あなたは無視するようにintパラメータを渡していますか? – Zeromus

+0

それを私に指摘してくれてありがとう。コンストラクタのどれがセットを期待しているか教えてください。もう一度 – olammy

+0

していただきありがとうございます。 – Zeromus

答えて

0

ApplicationListenerを実装してコードを記述するクラスを作成します。このクラスは、web.xmlのエントリに応じて、起動時に複数回呼び出されることがあります。 挿入中に慎重になると、重複したデータになる可能性があります。

public final class SystemStartUp implements ApplicationListener<ContextRefreshedEvent> { 
    @Override 
    public void onApplicationEvent(ContextRefreshedEvent event) { 
     //put startup code here 
    } 
}