Answer the question
In order to leave comments, you need to log in
Hibernate lifecycle callbacks, how to use it correctly?
When initializing the Company object and persistence operation, my callbacks don't work.. what could be the problem?
What is missing in this code? Any advice.. Help Company.class
:
@Entity
@Table(name = "companies")
@EntityListeners(CompanyListener.class)
public class Company {
public Company() {}
public User getCompany_user() {
return company_user;
}
public void setCompany_user(User company_user) {
this.company_user = company_user;
}
@Transient
private User company_user;
public Company(long id ,String compName, String password, String email) {
setId(id);
setCompName(compName);
setPassword(password);
setEmail(email);
}
public Collection<Coupon> getCompany_coupons() {
return company_coupons;
}
public void setCompany_coupons(Collection<Coupon> company_coupons) {
this.company_coupons = company_coupons;
}
@OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
@JoinTable(name = "company_coupons" , inverseJoinColumns = @JoinColumn(name = "COUPON_ID") , joinColumns = @JoinColumn(name = "COMPANY_ID"))
private Collection <Coupon> company_coupons;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Company company = (Company) o;
if (id != company.id) return false;
if (!compName.equals(company.compName)) return false;
if (!password.equals(company.password)) return false;
return email.equals(company.email);
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + compName.hashCode();
result = 31 * result + password.hashCode();
result = 31 * result + email.hashCode();
return result;
}
@Id
@Column(name = "COMPANY_ID")
private long id;
@Column(name = "COMPANY_NAME" , unique = true , nullable = false)
private String compName;
@Column(name = "COMPANY_PASSWORD" , unique = true , nullable = false)
private String password;
@Column(name = "COMPANY_EMAIL" , unique = true , nullable = false)
private String email;
@Override
public String toString() {
return "Company [id=" + id + ", compName=" + compName + ", password=" + password + ", email=" + email +"]";
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCompName() {
return compName;
}
public void setCompName(String compName) {
this.compName = compName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public class CompanyListener {
@PostPersist
public void persistListener(){
System.out.println();
System.out.println("Post persist Listener invoked!");
}
@PostUpdate
public void updateListener(){
System.out.println();
System.out.println("Update Listener invoked!");
}
@PostRemove
public void removeListener(){
System.out.println();
System.out.println("Remove Listener invoked!");
}
private String getCurrentTime(){
return LocalDateTime.now().toString();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question