JPA, Primary Key wird nicht in relationstabelle erstellt

shoehmi

Neues Mitglied
hallo leute,

bin neuling im JPA bereich. Schreibe gerade eine Anwendung wo ich JPA verwende. Als Framework nutze ich "Play". Das Framework liefert eine Klasse "Model" die auf JPA/Hibernate aufbaut und einiges vereinfacht.

Mein Problem:

Ich habe probleme mit zwei Tabellen:
- PeriodicalPayment
- Transaction

Transaction speichert einfach nur Kontobewegungen aller Art.
PeriodicalPayment speichert Daueraufträge.

Da die Transactiontabelle unabhängig bleiben soll darf da kein PeriodicalPayment schlüssel rein.
Also brauch ich eine neue Tabelle die mir JPA automatisch erstellt über eine Relation.
Er legt mir auch eine Tabelle für die Realtion an aber leider ohne Primarykey. Und das meckert
mir MySQL an und ist auch nicht schön.


wie kann ich einen Primarykey definieren ohne eine extra Klasse "PeriodicalPaymentTransaction" zu benutzen???

Klasse PeriodicalPayment:

Java:
package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;

import java.util.*;

@Entity
public class PeriodicalPayment extends Model {
	
    @ManyToOne
    public Account account;
    
    @OneToMany(cascade = CascadeType.ALL)
    public List<Transaction> transactions;
    
	public int amount;	
	public String currency;
	public Date startAt;
    public Date createdAt;		
    public int periodType;
    
    public PeriodicalPayment(Account account, int amount, String currency, Date startAt, int periodType ) {
        this.account = account;
    	this.transactions = new ArrayList<Transaction>();        
        this.amount = amount;
        this.currency = currency;
        this.startAt = startAt;
        this.createdAt = new Date();        
        this.periodType = periodType;	        
    }
    
}

Klasse Transaction:

Java:
package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;

import java.util.*;

@Entity
public class Transaction extends Model {
	public int amount;	
	public String currency;
    public Date createdAt;		
    
    @ManyToOne
    public Account account;

    public Transaction(Account account, int amount, String currency) {
        this.account = account;
        this.amount = amount;
        this.currency = currency;
        this.createdAt = new Date();	        
    }
    
}

mfg
 
Hi,

also einen primarykey kannst du folgendermaßen definieren:
Java:
public class deineKlasse {
@Id
@Column (name = "deineKlasse_ID")
@Generatedvalue (strategy= "such dir eine aus")
private long id;
...
}

wenn du in beide klassen so einen primary key definiert hast, kannst du bei deiner liste transactions noch die
@JoinTable Annotation hinzufügen, das sieht dann so aus:
Java:
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(
   name = "irgendEinTabellenName",
   joincolumns= @JoinColumn(name = "PPID",referencedColumnName="PP_ID"), //das zweite ist der Columnname deiner Id
   inversejoincolumns=@JoinColumn(name ="TransactionID", referencedColumnName="Trans_ID"))
   public List<Transaction> transactions;

nur @JoinColumn anstatt @JoinTable würde denke ich auch gehen, aber JoinTable ist meiner meinung nach leichter lesbar da alle information dortstehen.

gruß
 
Hi,

also einen primarykey kannst du folgendermaßen definieren:
Java:
public class deineKlasse {
@Id
@Column (name = "deineKlasse_ID")
@Generatedvalue (strategy= "such dir eine aus")
private long id;
...
}
...

Bääääm! Benutz bitte keine primitiven Datentypen! Hier im Bespiel
Java:
private long id;
sondern
Java:
private Long id;
 

Zurück
Oben