NamedQueries for publication entities
parent
6d457cb538
commit
3c7b4b56d9
|
|
@ -23,6 +23,8 @@ import javax.persistence.GenerationType;
|
|||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
|
|
@ -35,6 +37,17 @@ import static org.scientificcms.publications.SciPublicationsConstants.*;
|
|||
@Entity
|
||||
@Table(name = "JOURNALS", schema = DB_SCHEMA)
|
||||
@Audited
|
||||
@NamedQueries({
|
||||
@NamedQuery(
|
||||
name = "Journal.findByUuid",
|
||||
query = "SELECT j FROM Journal j WHERE j.uuid = :uuid"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "Journal.findByTitle",
|
||||
query = "SELECT j FROM Journal j "
|
||||
+ "WHERE LOWER(j.title) LIKE CONCAT('%', :title, '%')"
|
||||
)
|
||||
})
|
||||
public class Journal implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
|
|
|||
|
|
@ -7,13 +7,18 @@ package org.scientificcms.publications;
|
|||
|
||||
import org.libreccm.core.AbstractEntityRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
public class JournalRepository extends AbstractEntityRepository<Long, Journal>{
|
||||
public class JournalRepository extends AbstractEntityRepository<Long, Journal> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
|
@ -41,6 +46,28 @@ public class JournalRepository extends AbstractEntityRepository<Long, Journal>{
|
|||
protected void initNewEntity(final Journal entity) {
|
||||
entity.setUuid(UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public Optional<Journal> findByUuid(final String uuid) {
|
||||
|
||||
try {
|
||||
return Optional.of(
|
||||
getEntityManager()
|
||||
.createNamedQuery("Journal.findByUuid", Journal.class)
|
||||
.setParameter("uuid", uuid)
|
||||
.getSingleResult()
|
||||
);
|
||||
} catch (NoResultException ex) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public List<Journal> findByTitle(final String title) {
|
||||
return getEntityManager()
|
||||
.createNamedQuery("Journal.findByTitle", Journal.class)
|
||||
.setParameter("title", title)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
*/
|
||||
package org.scientificcms.publications;
|
||||
|
||||
import org.hibernate.annotations.NamedQueries;
|
||||
import org.hibernate.annotations.NamedQuery;
|
||||
import org.hibernate.envers.Audited;
|
||||
import org.libreccm.l10n.LocalizedString;
|
||||
|
||||
|
|
@ -43,6 +45,50 @@ import static org.scientificcms.publications.SciPublicationsConstants.*;
|
|||
@Table(name = "PUBLICATIONS", schema = DB_SCHEMA)
|
||||
@Inheritance(strategy = InheritanceType.JOINED)
|
||||
@Audited
|
||||
@NamedQueries({
|
||||
@NamedQuery(
|
||||
name = "Publication.findByIdAndType",
|
||||
query = "SELECT p FROM Publication p "
|
||||
+ "WHERE p.publicationId = :publicationId "
|
||||
+ "AND TYPE(p) = :type"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "Publication.findByUuid",
|
||||
query = "SELECT p FROM Publication p WHERE p.uuid = :uuid"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "Publication.findByUuidAndType",
|
||||
query = "SELECT p FROM Publication p "
|
||||
+ "WHERE p.uuid = :uuid "
|
||||
+ "AND TYPE(p) = :type"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "Publication.findByTitle",
|
||||
query = "SELECT DISTINCT p "
|
||||
+ "FROM Publication p JOIN p.title.values t "
|
||||
+ "WHERE LOWER(t) LIKE CONCAT('%', :title, '%')"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "Publication.findByTitleAndType",
|
||||
query = "SELECT DISTINCT p "
|
||||
+ "FROM Publication p JOIN p.title.values t "
|
||||
+ "WHERE LOWER(t) LIKE CONCAT('%', :title, '%') "
|
||||
+ "AND TYPE(p) = :type"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "Publication.findByAuthor",
|
||||
query = "SELECT DISTINCT p "
|
||||
+ "FROM Publication p JOIN p.authorships a "
|
||||
+ "WHERE a.author = :author"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "Publication.findByAuthorAndType",
|
||||
query = "SELECT DISTINCT p "
|
||||
+ "FROM Publication p JOIN p.authorships a "
|
||||
+ "WHERE a.author = :author "
|
||||
+ "AND TYPE(p) = :type"
|
||||
)
|
||||
})
|
||||
public class Publication implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
|
@ -58,7 +104,7 @@ public class Publication implements Serializable {
|
|||
@Column(name = "YEAR_OF_PUBLICATION")
|
||||
private Integer yearOfPublication;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL,
|
||||
@OneToMany(cascade = CascadeType.ALL,
|
||||
fetch = FetchType.LAZY,
|
||||
mappedBy = "author",
|
||||
orphanRemoval = true)
|
||||
|
|
|
|||
|
|
@ -6,17 +6,22 @@
|
|||
package org.scientificcms.publications;
|
||||
|
||||
import org.libreccm.core.AbstractEntityRepository;
|
||||
import org.librecms.assets.Person;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
@RequestScoped
|
||||
public class PublicationRepository
|
||||
public class PublicationRepository
|
||||
extends AbstractEntityRepository<Long, Publication> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
|
@ -40,11 +45,96 @@ public class PublicationRepository
|
|||
public Class<Publication> getEntityClass() {
|
||||
return Publication.class;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void initNewEntity(final Publication entity) {
|
||||
final String uuid = UUID.randomUUID().toString();
|
||||
entity.setUuid(uuid);
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public <T extends Publication> Optional<T> findByIdAndType(
|
||||
final long publicationId, final Class<T> type
|
||||
) {
|
||||
try {
|
||||
return Optional.of(
|
||||
getEntityManager()
|
||||
.createNamedQuery("Publication.findByIdAndType", type)
|
||||
.setParameter("publicationId", publicationId)
|
||||
.setParameter("type", type)
|
||||
.getSingleResult()
|
||||
);
|
||||
} catch (NoResultException ex) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public Optional<Publication> findByUuid(final String uuid) {
|
||||
|
||||
try {
|
||||
return Optional.of(
|
||||
getEntityManager()
|
||||
.createNamedQuery("Publication.findByUuid",
|
||||
Publication.class)
|
||||
.setParameter("uuid", uuid)
|
||||
.getSingleResult()
|
||||
);
|
||||
} catch (NoResultException ex) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public <T extends Publication> Optional<T> findByUuidAndType(
|
||||
final String uuid, final Class<T> type
|
||||
) {
|
||||
try {
|
||||
return Optional.of(
|
||||
getEntityManager()
|
||||
.createNamedQuery("Publication.findByUuidAndType", type)
|
||||
.setParameter("uuid", uuid)
|
||||
.setParameter("type", type)
|
||||
.getSingleResult()
|
||||
);
|
||||
} catch (NoResultException ex) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Publication> findByTitle(final String title) {
|
||||
|
||||
return getEntityManager()
|
||||
.createNamedQuery("Publication.findByTitle", Publication.class)
|
||||
.setParameter("title", title)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public <T extends Publication> List<T> findByTitleAndType(
|
||||
final String title, final Class<T> type
|
||||
) {
|
||||
return getEntityManager()
|
||||
.createNamedQuery("Publication.findByTitleAndType", type)
|
||||
.setParameter("title", title)
|
||||
.setParameter("type", type)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public List<Publication> findByAuthor(final Person author) {
|
||||
return getEntityManager()
|
||||
.createNamedQuery("Publication.findByAuthor", Publication.class)
|
||||
.setParameter("author", author)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
public <T extends Publication> List<T> findByAuthorAndType(
|
||||
final Person author, final Class<T> type
|
||||
) {
|
||||
return getEntityManager()
|
||||
.createNamedQuery("Publication.findByAuthorAndType", type)
|
||||
.setParameter("author", author)
|
||||
.setParameter("type", type)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ import javax.persistence.Entity;
|
|||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import static org.scientificcms.publications.SciPublicationsConstants.*;
|
||||
|
|
@ -28,6 +30,29 @@ import static org.scientificcms.publications.SciPublicationsConstants.*;
|
|||
@Entity
|
||||
@Table(name = "PUBLICATIONS_WITH_PUBLISHER")
|
||||
@Audited
|
||||
@NamedQueries({
|
||||
@NamedQuery(
|
||||
name = "PublicationWithPublisher.findByPublisher",
|
||||
query = "SELECT p FROM PublicationWithPublisher p "
|
||||
+ "WHERE p.publisher = :publisher"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "PublicationWithPublisher.findByPublisherAndType",
|
||||
query = "SELECT p FROM PublicationWithPublisher p "
|
||||
+ "WHERE p.publisher = :publisher "
|
||||
+ "AND TYPE(p) = :type"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "PublicationWithPublisher.findByISBN10",
|
||||
query = "SELECT p FROM PublicationWithPublisher p "
|
||||
+ "WHERE p.isbn10 = :isbn"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "PublicationWithPublisher.findByISBN13",
|
||||
query = "SELECT p FROM PublicationWithPublisher p "
|
||||
+ "WHERE p.isbn13 = :isbn"
|
||||
)
|
||||
})
|
||||
public class PublicationWithPublisher extends Publication {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.scientificcms.publications;
|
||||
|
||||
import org.libreccm.core.AbstractEntityRepository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
@RequestScoped
|
||||
public class PublicationWithPublisherRepository
|
||||
extends AbstractEntityRepository<Long, PublicationWithPublisher>
|
||||
implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Inject
|
||||
private PublicationRepository publicationRepository;
|
||||
|
||||
@Override
|
||||
public Class<PublicationWithPublisher> getEntityClass() {
|
||||
return PublicationWithPublisher.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdAttributeName() {
|
||||
return publicationRepository.getIdAttributeName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getIdOfEntity(final PublicationWithPublisher entity) {
|
||||
return publicationRepository.getIdOfEntity(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew(final PublicationWithPublisher entity) {
|
||||
return publicationRepository.isNew(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initNewEntity(final PublicationWithPublisher entity) {
|
||||
publicationRepository.initNewEntity(entity);
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public List<PublicationWithPublisher> findByPublisher(
|
||||
final Publisher publisher
|
||||
) {
|
||||
return getEntityManager()
|
||||
.createNamedQuery("PublicationWithPublisher.findByPublisher",
|
||||
PublicationWithPublisher.class)
|
||||
.setParameter("publisher", publisher)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public <T extends PublicationWithPublisher> List<T> findByPublisherAndType(
|
||||
final Publisher publisher, final Class<T> type
|
||||
) {
|
||||
return getEntityManager()
|
||||
.createNamedQuery("PublicationWithPublisher.findByPublisherAndType",
|
||||
type)
|
||||
.setParameter("publisher", publisher)
|
||||
.setParameter("type", type)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public Optional<PublicationWithPublisher> findByIsbn10(final String isbn) {
|
||||
try {
|
||||
return Optional.of(
|
||||
getEntityManager()
|
||||
.createNamedQuery("PublicationWithPublisher.findByISBN10",
|
||||
PublicationWithPublisher.class)
|
||||
.setParameter("isbn", isbn)
|
||||
.getSingleResult()
|
||||
);
|
||||
} catch (NoResultException ex) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public Optional<PublicationWithPublisher> findByIsbn13(final String isbn) {
|
||||
try {
|
||||
return Optional.of(
|
||||
getEntityManager()
|
||||
.createNamedQuery("PublicationWithPublisher.findByISBN13",
|
||||
PublicationWithPublisher.class)
|
||||
.setParameter("isbn", isbn)
|
||||
.getSingleResult()
|
||||
);
|
||||
} catch (NoResultException ex) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,6 +18,8 @@ import javax.persistence.Entity;
|
|||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
|
|
@ -30,6 +32,17 @@ import static org.scientificcms.publications.SciPublicationsConstants.*;
|
|||
@Entity
|
||||
@Table(name = "PUBLISHERS", schema = DB_SCHEMA)
|
||||
@Audited
|
||||
@NamedQueries({
|
||||
@NamedQuery(
|
||||
name = "Publisher.findByUuid",
|
||||
query = "SELECT p FROM Publisher p WHERE p.uuid = :uuid"
|
||||
),
|
||||
@NamedQuery(
|
||||
name = "Publisher.findByName",
|
||||
query = "SELECT p FROM Publisher p "
|
||||
+ "WHERE p.name LIKE CONCAT('%', :name, '%')"
|
||||
)
|
||||
})
|
||||
public class Publisher implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
|
|
|||
|
|
@ -7,8 +7,13 @@ package org.scientificcms.publications;
|
|||
|
||||
import org.libreccm.core.AbstractEntityRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
|
|
@ -43,4 +48,26 @@ public class PublisherRepository
|
|||
entity.setUuid(UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public Optional<Publisher> findByUuid(final String uuid) {
|
||||
try {
|
||||
return Optional.of(
|
||||
getEntityManager()
|
||||
.createNamedQuery("Publisher.findByUuid", Publisher.class)
|
||||
.setParameter("uuid", uuid)
|
||||
.getSingleResult()
|
||||
);
|
||||
} catch (NoResultException ex) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public List<Publisher> findByName(final String name) {
|
||||
return getEntityManager()
|
||||
.createNamedQuery("Publisher.findByName", Publisher.class)
|
||||
.setParameter("name", name)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue