2016-11-26 1 views
2

Ich habe eine Anwalts-Tabelle mit der ID (int) als Primärschlüssel und einer Länder-Tabelle mit country_code (String) als Primärschlüssel. Ich möchte eine dritte Tabelle mit @JoinTable Annotation im Ruhezustand mit zwei Fremdschlüssel erstellen. Aber wenn ich es starte, kommt folgender Fehler. Nicht sicher, wie eine Zeichenfolge und ein int als Fremdschlüssel in der dritten Tabelle zugeordnet werden.Illegaler Versuch, eine Nicht-Sammlung als @OneToMany, @ManyToMany oder @CollectionOfElements zuzuordnen

Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.test.common.entities.Country.lawyer 

Dies ist mein Code

@Entity 
@Table(name = "lawyer") 
public class Lawyer { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "lawyer_batch_no") 
    private int lawyerbatchNo; 

@ManyToOne(targetEntity = Country.class, cascade = { CascadeType.ALL }) 
    @JoinTable(name = "lawyer_cscd", joinColumns = { 
      @JoinColumn(name = "lawyer_batch_no", referencedColumnName = "lawyer_batch_no") }, inverseJoinColumns = { 
        @JoinColumn(name = "country_code", referencedColumnName = "country_code") }) 
    private Country country; 

getter setter... 
} 

@Entity 
@Table(name = "country") 
public class Country { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name = "country_code") 
    protected String country_code; 

    @Column(name = "abbreviation") 
    protected String abbreviation; 

    @Column(name = "name", nullable = false) 
    protected String name; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country") 
    protected Set<State> state = new HashSet<State>(); 

    @OneToMany(targetEntity = Lawyer.class, cascade = { CascadeType.ALL }, orphanRemoval = true) 
    @JoinTable(name = "lawyer_cscd", joinColumns = { 
      @JoinColumn(name = "country_code", referencedColumnName = "country_code") }, inverseJoinColumns = { 
        @JoinColumn(name = "lawyer_batch_no", referencedColumnName = "lawyer_batch_no") }) 
    private Lawyer lawyer; 

getter setter.... 
} 
+1

Der Fehler zeigt, dass 'privaten Rechtsanwalt Anwalt;' braucht eine Sammlung zu sein, da es sich um eine OneToMany Beziehung ist – baao

+0

@baao Dank für Anzeichen dafür, dass ein dummer Fehler von meiner Seite Dank ist zum helfen – henrycharles

Antwort

3

Der Fehler zeigt an, dass private Lawyer lawyer Bedarf eine Sammlung zu sein, da es sich um eine @OneToMany Beziehung ist. In der Country Klasse sollte die letzte Beziehung

@OneToMany(targetEntity = Lawyer.class, cascade = { CascadeType.ALL }, orphanRemoval = true) 
@JoinTable(name = "lawyer_cscd", joinColumns = { 
    @JoinColumn(name = "country_code", referencedColumnName = "country_code") }, inverseJoinColumns = { 
    @JoinColumn(name = "lawyer_batch_no", referencedColumnName = "lawyer_batch_no") }) 
private Set<Lawyer> lawyer; 
// or a Collection/List/etc. 
+0

Ich habe noch eine Frage http://stackoverflow.com/questions/40829022/field-district-code-doesnt-have-a-default-value?noredirect=1#comment68876061_40829022 Ich bekomme Fehler können Sie bitte in es und lass es mich wissen – henrycharles

Verwandte Themen