Related differences

JDBC vs JPAJPA vs Hibernate

Ques 36. What is Embeddable classes?

  • Entities may use persistent fields, persistent properties, or a combination of both.
  • If the mapping annotations are applied to the entity's instance variables, the entity uses persistent fields.
  • If the mapping annotations are applied to the entity's getter methods for JavaBeans-style properties, the entity uses persistent properties.

Is it helpful? Add Comment View Comments
 

Ques 37. What is JPQL?

JPQL (Java Persistence Query Language) offers an object-oriented syntax for expressing queries that are very similar to SQL. The language is interpreted at runtime, which means you cannot use the compiler to verify the correctness and integrity of a query. To address this limitation, Hibernate includes a Criteria API, which allows queries to be expressed programmatically.

Is it helpful? Add Comment View Comments
 

Ques 38. Example of an Entity with Embedded class.

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "categoryparticipant")
@NamedQueries({
@NamedQuery(name = "Categoryparticipant.getCategoriesOfParticipant", query = "SELECT cp.category from Categoryparticipant cp where cp.participant.participantid= :participantid")
})
public class Categoryparticipant implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected CategoryparticipantPK categoryparticipantPK;
@Basic(optional = false)
@Column(name = "CreationDate")
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@JoinColumn(name = "ParticipantId", referencedColumnName = "ParticipantId", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Participant participant;
@JoinColumn(name = "CategoryId", referencedColumnName = "CategoryId", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Category category;

public Categoryparticipant() {
}

public Categoryparticipant(CategoryparticipantPK categoryparticipantPK) {
this.categoryparticipantPK = categoryparticipantPK;
}

public Categoryparticipant(CategoryparticipantPK categoryparticipantPK, Date creationDate) {
this.categoryparticipantPK = categoryparticipantPK;
this.creationDate = creationDate;
}

public Categoryparticipant(int categoryId, int participantId) {
this.categoryparticipantPK = new CategoryparticipantPK(categoryId, participantId);
}

public CategoryparticipantPK getCategoryparticipantPK() {
return categoryparticipantPK;
}

public void setCategoryparticipantPK(CategoryparticipantPK categoryparticipantPK) {
this.categoryparticipantPK = categoryparticipantPK;
}

public Date getCreationDate() {
return creationDate;
}

public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}

public Participant getParticipant() {
return participant;
}

public void setParticipant(Participant participant) {
this.participant = participant;
}

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

@Override
public int hashCode() {
int hash = 0;
hash += (categoryparticipantPK != null ? categoryparticipantPK.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Categoryparticipant)) {
return false;
}
Categoryparticipant other = (Categoryparticipant) object;
if ((this.categoryparticipantPK == null && other.categoryparticipantPK != null) || (this.categoryparticipantPK != null && !this.categoryparticipantPK.equals(other.categoryparticipantPK))) {
return false;
}
return true;
}

@Override
public String toString() {
return "com.xchanging.entity.jpa.Categoryparticipant[categoryparticipantPK=" + categoryparticipantPK + "]";
}

}

Is it helpful? Add Comment View Comments
 

Ques 39. Give an example of Embeddable class for previous question Categoryparticipant entity.

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class CategoryparticipantPK implements Serializable {
@Basic(optional = false)
@Column(name = "CategoryId")
private int categoryId;
@Basic(optional = false)
@Column(name = "ParticipantId")
private int participantId;

public CategoryparticipantPK() {
}

public CategoryparticipantPK(int categoryId, int participantId) {
this.categoryId = categoryId;
this.participantId = participantId;
}

public int getCategoryId() {
return categoryId;
}

public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}

public int getParticipantId() {
return participantId;
}

public void setParticipantId(int participantId) {
this.participantId = participantId;
}

@Override
public int hashCode() {
int hash = 0;
hash += (int) categoryId;
hash += (int) participantId;
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof CategoryparticipantPK)) {
return false;
}
CategoryparticipantPK other = (CategoryparticipantPK) object;
if (this.categoryId != other.categoryId) {
return false;
}
if (this.participantId != other.participantId) {
return false;
}
return true;
}

@Override
public String toString() {
return "com.xchanging.entity.jpa.CategoryparticipantPK[categoryId=" + categoryId + ", participantId=" + participantId + "]";
}

}

Is it helpful? Add Comment View Comments
 

Ques 40. Insert a record mechanism using JPA. Provide an example.

@Override
@Transactional
public void create(Category entity) throws MeetingAppDAOException {
  try {
    logger.info("Enter - create()");
    super.create(entity);
    logger.info("Exit - create()");
  } catch (PersistenceException exception) {
    logger.error("create()::REASON OF EXCEPTION=" + exception.getMessage(), e);
  }
}

Is it helpful? Add Comment View Comments
 

Most helpful rated by users: