Announcement

Collapse
No announcement yet.

Relationships klappen nicht

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Relationships klappen nicht

    Hallo miteinander,

    ich bin neu hier im Forum und auch auf dem Gebiet der EJBs. Ich versuche mich seit einigen Tagen an einer Übung und trotz Studium der Dokumentationen und ausgiebiger Recherchen im Internet will es einfach nicht klappen. Deshalb möchte ich meinen Code nun euren kundigen Augen vorlegen. Konkret ist das Problem, dass die Entities zwar persistent gemacht werden, jedoch scheinbar keine Relationships angelegt werden. Jedenfalls klappt cascaded delete nicht. Hier also mein Code und vielen Dank im Voraus für eure Tipps.

    Customer.java:
    Code:
    @Entity
    @Table(name="Customer")
    public class Customer implements Serializable {
    	private static final long serialVersionUID = 1L;
    	private int customerId;
    	private String firstName;
    	private String lastName;
    	private String address;
    	private int zip;
    	private String city;
    	private int birthdate;
    	public enum Gender{FEMALE, MALE};
    	private Gender gender;
    	private String nationality;
    	private List<Policy> policies;
    
    	public Customer(){}
    	
    	public Customer(int customerId, String firstName, String lastName, String address, int zip, String city, int birthdate, Gender gender, String nationality) {
    		this.customerId = customerId;
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.address = address;
    		this.zip = zip;
    		this.city = city;
    		this.birthdate = birthdate;
    		this.gender = gender;
    		this.nationality = nationality;
    	}
    
    	@Id
    	public int getCustomerId() {
    		return customerId;
    	}
    
    	public void setCustomerId(int customerId) {
    		this.customerId = customerId;
    	}
    
    	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 getAddress() {
    		return address;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    
    	public int getZip() {
    		return zip;
    	}
    
    	public void setZip(int zip) {
    		this.zip = zip;
    	}
    
    	public String getCity() {
    		return city;
    	}
    
    	public void setCity(String city) {
    		this.city = city;
    	}
    
    	public int getBirthdate() {
    		return birthdate;
    	}
    
    	public void setBirthdate(int birthdate) {
    		this.birthdate = birthdate;
    	}
    
    	public Gender getGender() {
    		return gender;
    	}
    
    	public void setGender(Gender gender) {
    		this.gender = gender;
    	}
    
    	public String getNationality() {
    		return nationality;
    	}
    
    	public void setNationality(String nationality) {
    		this.nationality = nationality;
    	}
    	
    	@OneToMany(cascade={CascadeType.ALL}, mappedBy="customer")
    	public List<Policy> getPolicies() {
    		return policies;
    	}
    
    	public void setPolicies(List<Policy> policies) {
    		this.policies = policies;
    	}
    }
    Policy.java:
    Code:
    @Entity
    @Table(name="Policy")
    public class Policy implements Serializable {
    	private static final long serialVersionUID = 1L;
    	private int policyId;
    	private int customerId;
    	public enum Type {LIFE, HOUSEHOLD, MOTOR, LIABILITY};
    	private Type type;
    	private float level;
    	private int coverageAmount;
    	public enum Status {VALID, CANCELLED};
    	private Status status;
    	private String cancellationComment;
    	private Customer customer;
    	private List<Claim> claims;
    	
    	public Policy(){}
    	
    	public Policy(int policyId, Type type, float level, int coverageAmount) {
    		this.policyId = policyId;
    		this.type = type;
    		this.level = level;
    		this.coverageAmount = coverageAmount;
    		this.status = Status.VALID;
    	}
    
    	@Id
    	public int getPolicyId() {
    		return policyId;
    	}
    
    	public void setPolicyId(int policyId) {
    		this.policyId = policyId;
    	}
    	
    	public int getCustomerId() {
    		return customerId;
    	}
    
    	public void setCustomerId(int customerId) {
    		this.customerId = customerId;
    	}
    	
    	public Type getType() {
    		return type;
    	}
    
    	public void setType(Type type) {
    		this.type = type;
    	}
    
    	public float getLevel() {
    		return level;
    	}
    
    	public void setLevel(float level) {
    		this.level = level;
    	}
    
    	public int getCoverageAmount() {
    		return coverageAmount;
    	}
    
    	public void setCoverageAmount(int coverageAmount) {
    		this.coverageAmount = coverageAmount;
    	}
    
    	public Status getStatus() {
    		return status;
    	}
    
    	public void setStatus(Status status) {
    		this.status = status;
    	}
    
    	public String getCancellationComment() {
    		return cancellationComment;
    	}
    
    	public void setCancellationComment(String cancellationComment) {
    		this.cancellationComment = cancellationComment;
    	}
    	
    	@ManyToOne
    	@JoinColumn(name="CUSTOMER_ID")
    	public Customer getCustomer() {
    		return customer;
    	}
    
    	public void setCustomer(Customer customer) {
    		this.customer = customer;
    	}
    	
    	@OneToMany(cascade={CascadeType.ALL}, mappedBy="policy")
    	public List<Claim> getClaims() {
    		return claims;
    	}
    
    	public void setClaims(List<Claim> claims) {
    		this.claims = claims;
    	}
    }
    Claim.java:
    Code:
    @Entity
    @Table(name="Claim")
    public class Claim implements Serializable {
    	private static final long serialVersionUID = 1L;
    	private int claimId;
    	private int policyId;
    	private Type typeOfDamage;
    	private int estimatedAmountOfDamage;
    	public enum Status {OPEN, IN_PROGRESS, APPROVED, DENIED, CLOSED};
    	private Status status;
    	private String approvalReason;
    	private int amountPaidOut;
    	private Policy policy;
    	
    	public Claim (){}
    	
    	public Claim(int claimId, int policyId, Type typeOfDamage, int estimatedAmountOfDamage){
    		this.claimId = claimId;
    		this.policyId = policyId;
    		this.typeOfDamage = typeOfDamage;
    		this.estimatedAmountOfDamage = estimatedAmountOfDamage;
    		this.status = Status.OPEN;
    	}
    
    	@Id
    	public int getClaimId() {
    		return claimId;
    	}
    
    	public void setClaimId(int claimId) {
    		this.claimId = claimId;
    	}
    	
    	public int getPolicyId() {
    		return policyId;
    	}
    
    	public void setPolicyId(int policyId) {
    		this.policyId = policyId;
    	}
    	
    	public Type getTypeOfDamage() {
    		return typeOfDamage;
    	}
    
    	public void setTypeOfDamage(Type typeOfDamage) {
    		this.typeOfDamage = typeOfDamage;
    	}
    
    	public int getEstimatedAmountOfDamage() {
    		return estimatedAmountOfDamage;
    	}
    
    	public void setEstimatedAmountOfDamage(int estimatedAmountOfDamage) {
    		this.estimatedAmountOfDamage = estimatedAmountOfDamage;
    	}
    
    	public Status getStatus() {
    		return status;
    	}
    
    	public void setStatus(Status status) {
    		this.status = status;
    	}
    
    	public String getApprovalReason() {
    		return approvalReason;
    	}
    
    	public void setApprovalReason(String approvalReason) {
    		this.approvalReason = approvalReason;
    	}
    
    	public int getAmountPaidOut() {
    		return amountPaidOut;
    	}
    
    	public void setAmountPaidOut(int amountPaidOut) {
    		this.amountPaidOut = amountPaidOut;
    	}
    	
    	@ManyToOne
    	@JoinColumn(name="POLICY_ID")
    	public Policy getPolicy() {
    		return policy;
    	}
    
    	public void setPolicy(Policy policy) {
    		this.policy = policy;
    	}
    }

  • #2
    Wahrscheinlich wird der vom Persistenzframework generierte Spaltenname für die Id des Cusotmers nicht identisch sein mit dem in Policy vergebenen JoinColumnnamen
    Christian

    Comment


    • #3
      moin,

      StackTrace vom aufgetretenen Fehler wäre auch hilfreich, um das Problem lokalisieren.

      Comment

      Working...
      X