Without Book Interview Questions and Answers | Online Test | Moct Test
Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Institute Training Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

JPA Interview Questions and Answers

Ques 11. Example of an Entity with Embedded class.

Ans. 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 12. Give an example of Embeddable class for previous question Categoryparticipant entity.
Ans. 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 13. Insert a record mechanism using JPA.
Ans. @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
Ques 14. How to fetch a record using NamedQuery?
Ans. public Category findByCategoryId(Long categoryId) {
try {
logger.info(\"Enter - findByCategoryId()\");
Query lQuery = (Query) em.createNamedQuery(\"Category.findByCategoryId\");
Integer intValue = categoryId.intValue();
lQuery.setParameter(\"categoryId\", intValue);
Category category = (Category) lQuery.getSingleResult();
logger.info(\"Exit - findByCategoryId\");
return category;
} catch (PersistenceException exception) {
logger.debug(exception.getCause().getStackTrace());
logger.error(\"CategoryDaoImpl::maxCategoryId()::REASON OF EXCEPTION=\" + exception.getMessage() + exception.getCause());
} finally {
closeEntityManager();
}

}
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2017 WithoutBook