Load tables only with some specific conditions (#Where) - jpa

What I'm trying to do is to load only promotions with promotion.enabled=1 AND PromotionType.enabled=1.
I tried to add the #Where annotation in both tables but when I set promotionType, enabled to 0 I´m getting an error.
On the other hand, I also tried to add the #WhereJoinTable clause to promotionType but I'm not getting the expected result. Any help?
The first one:
#Entity
#Table(name = "HOTEL_PROMOTION")
#Where(clause = "enabled=1")
public class Promotion implements Serializable {
private static final long serialVersionUID = 257070400893576505L;
#Id
#Column(name = "PROMOTION_ID")
private Long id;
#Column(name = "CODE")
private String code;
#Column(name = "NAME")
private String name;
#Column(name = "PRIORITY")
private Long priority;
#ManyToOne
#JoinColumn(name = "PROMOTION_TYPE_ID")
#WhereJoinTable(clause = "enabled=1")
private PromotionType promotionType;
#Column(name = "ENABLED")
private Boolean enabled;
}
The second one:
#Entity
#Table(name = "HOTEL_PROMOTION_TYPE")
public class PromotionType implements Serializable {
private static final long serialVersionUID = -8359165117733458987L;
#Id
#Column(name = "PROMOTION_TYPE_ID")
private Long id;
#Column(name = "CODE")
private String code;
#Column(name = "NAME")
private String name;
#Column(name = "STYLE")
private String style;
#Column(name = "ENABLED")
private Boolean enabled;
}

Related

JPA Composite Key: Avoid Unnecessary of Table Creation

I am learning JPA.
I need to create 3 tables, product (pk => id), cart (pk => id), cart_details (pk also fk => product_id, cart_id).
The relation is : One cart can contain multiple cart_details, one cart_details can contain multiple product and one product can be put on multiple cart_details. I need only 3 tables, but JPA creates 4 tables for me: product, cart, cart_details, cart_details_product
#Entity
#Table(name = "product")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#NotBlank
#Size(max = 50)
private String name;
#Size(max = 300)
private String description;
#NotNull
private Double price;
private int qty;
#Column(name = "created_date")
private Date createdDate;
#Column(name = "updated_date")
private Date updatedDate;
}
#Entity
#Table(name = "cart")
public class Cart implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "total_price")
private double totalPrice;
#Column(name = "created_date")
private Date createdDate;
#Column(name = "updated_date")
private Date updatedDate;
}
#Entity
#Table(name = "cart_details")
public class CartDetails implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private CartDetailsId id;
#MapsId("cartId")
#ManyToOne
#JoinColumn(name = "cart_id", referencedColumnName = "id", insertable = false, updatable = false)
private Cart cart;
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinColumn(name = "product_id", referencedColumnName = "id")
private Set<Product> product;
private int quantity;
private double price;
}
#Embeddable
public class CartDetailsId implements Serializable {
private static final long serialVersionUID = 1L;
#Column(name = "cart_id")
private Long cartId;
#Column(name = "product_id")
private Long productId;
}
How to avoid creation of this table (cart_details_product)? I think i don't need this table.

Get only one field from the related table

I have following connection between tables Image:
#Entity
#Table(name = "image")
#Data
public class Image {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
private byte[] image;
#OneToOne(mappedBy = "avatar")
private Personal personal;
}
and Personal
#Entity
#Table(name = "personal")
#Data
public class Personal {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
private String position;
private String phone;
private String email;
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinColumn(name = "avatar_id", referencedColumnName = "id")
private Image avatar;
}
I want to get back from my service Personal entities with ONLY id field from Image table. Repositories and services are standard from tutorials - without extra code or overrides
If read-only is ok use a DTO or interface projection.

How to add new records to a field with #OneToOne in spring Data?

I am making a jsf + spring application.
The database contains a table of games and it is displayed on one of the pages of the site.
Each game has a genre list and development status. These fields are annotated with #OneToMany and #OneToOne respectively and are also tables in the database.
But here's the question: How do I add new games now? How do I initialize these fields? Because the only way I see is to create a new genre for a new game every time. That is, even if game A and games B are of the same genre, then I have to create two different unique genres, not one.
And how to initialize these fields from JSF?
For example from the <p: selectOneMenu> tag
game.java
#Setter
#Getter
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "game")
public class Game
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "name")
private String name;
#Column(name = "budget")
private String budget;
#Column(name = "profit")
private String profit;
#Column(name = "number")
private String number;
#OneToOne(optional = false, cascade = CascadeType.REFRESH)
#JoinColumn(name = "platform")
private Platform platform;
#OneToOne(optional = false, cascade = CascadeType.REFRESH)
#JoinColumn(name = "status")
private Status status;
#Column(name = "start")
private Date start;
#Column(name = "end")
private Date end;
#OneToMany(fetch = FetchType.EAGER)
#JoinTable(name = "game_genre",
joinColumns = #JoinColumn(name= "game_id"),
inverseJoinColumns = #JoinColumn(name= "genre_id"))
private List<Genre> listGenre;
public void update(Game new_game)
{
this.name = new_game.name;
this.budget = new_game.budget;
this.profit = new_game.profit;
this.number = new_game.number;
this.platform = new_game.platform;
this.status = new_game.status;
this.start = new_game.start;
this.end = new_game.end;
}
}
development status
#Setter
#Getter
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "status")
public class Status implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "Название")
private String Name;
#Column(name = "Описание")
private String description;
public void update(Status new_game)
{
this.description = new_game.description;
this.Name = new_game.Name;
}
}
genre:
#Setter
#Getter
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "genre")
public class Genre implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "name")
private String name;
#Column(name = "description")
private String description;
public void update(Genre new_game)
{
this.name = new_game.name;
this.description = new_game.description;
}
}
Bean
#Component(value = "listgames")
#SessionScope
public class GamesView {
#Autowired
private GamesService gamesService;
private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
private List<Game> All_games = new ArrayList<Game>();
private Game newGame=new Game();
public Game getNewGame() {
return newGame;
}
public void setNewGame(Game newGame) {
this.newGame = newGame;
}
public void onRowEdit(RowEditEvent event) {
Game new_game=(Game)event.getObject();
All_games.get(new_game.getId()-1).update(new_game);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO", "X/Y edit successful!");
FacesContext.getCurrentInstance().addMessage(null, msg);
int i=0;
i++;
}
public void createNew() {
gamesService.addBank(newGame);
newGame = new Game();
}
public List<Game> getAll_games() {
return gamesService.getAll();
}
public void setAll_games(List<Game> all_games) {
All_games = all_games;
}
}

The abstract schema type is unknown

I'm using Spring 3.2.3, JPA 2.1, JUnit 4.11. I'm trying to run a junit test and I keep getting the abstract schema type is unknown error. Here is my entity (truncated for space, it has all the getters and setters):
#Entity
#Table(name = "WEB_PROFILES")
public class TestWebProfile implements Serializable {
private static final long serialVersionUID = 1L;
#Transient
private String forward;
#Column(name = "ACCESS_FLAG")
private String accessFlag;
#Temporal(TemporalType.DATE)
#Column(name = "ACCESS_FLAG_UPD_DATE")
private Date accessFlagUpdDate;
#Column(name = "ACCESS_RESET_INTERVAL")
private BigDecimal accessResetInterval;
#Column(name = "ACCOUNT_TYPE")
private String accountType;
#Column(name = "CREATED_BY")
private String createdBy;
#Column(name = "E_MAIL")
private String eMail;
#Column(name = "FAILED_LOGIN_ATTEMPTS")
private BigDecimal failedLoginAttempts;
#Column(name = "FIRST_NAME")
private String firstName;
#Temporal(TemporalType.DATE)
#Column(name = "FROI_ACCESS_APPROVE_DENY_DATE")
private Date froiAccessApproveDenyDate;
#Column(name = "FROI_ACCESS_APPROVED_FLAG")
private String froiAccessApprovedFlag;
#Column(name = "FROI_ACCESS_REQUESTED")
private String froiAccessRequested;
#Column(name = "FROI_APPROVED_BY")
private String froiApprovedBy;
#Temporal(TemporalType.DATE)
#Column(name = "FROI_CONFIRM_EMAIL_SENT_DATE")
private Date froiConfirmEmailSentDate;
#Temporal(TemporalType.DATE)
#Column(name = "FROI_LETTER_SENT_DATE")
private Date froiLetterSentDate;
#Column(name = "LAST_LOGON_ADDR")
private String lastLogonAddr;
#Temporal(TemporalType.DATE)
#Column(name = "LAST_LOGON_DATE")
private Date lastLogonDate;
#Column(name = "LAST_NAME")
private String lastName;
#Column(name = "LAST_UPDATED_BY")
private String lastUpdatedBy;
#Column(name = "LAST_UPDATED_BY_NAME")
private String lastUpdatedByName;
#Column(name = "LAST_UPDATED_BY_SU_ID")
private BigDecimal lastUpdatedBySuId;
#Temporal(TemporalType.DATE)
#Column(name = "MAIL_SENT_DATE")
private Date mailSentDate;
#Temporal(TemporalType.DATE)
#Column(name = "MAINT_DATE")
private Date maintDate;
#Temporal(TemporalType.DATE)
#Column(name = "NEW_PIN_REQ_DATE")
private Date newPinReqDate;
#Column(name = "PASSWORD")
private String password;
#Transient
private String newPassword;
#Temporal(TemporalType.DATE)
#Column(name = "PASSWORD_UPD_DATE")
private Date passwordUpdDate;
#Column(name = "PHONE")
private String phone;
#Column(name = "PIN")
private String pin;
#Column(name = "POLICY_NUM")
private BigDecimal policyNo;
#Column(name = "PROFILE_CLASS_CODE")
private String profileClassCode;
#Temporal(TemporalType.DATE)
#Column(name = "PROFILE_REQ_DATE")
private Date profileReqDate;
#Temporal(TemporalType.DATE)
#Column(name = "PROFILE_UPDATE_DATE")
private Date profileUpdateDate;
#Column(name = "REMOTE_ADDR")
private String remoteAddr;
#Column(name = "SESSIONID")
private String sessionid;
#Column(name = "SUBSCRIBER_FLAG")
private String subscriberFlag;
#Column(name = "USER_ID")
private BigDecimal userId;
#Id
#Column(name = "USER_NO")
private BigDecimal userNo;
#Column(name = "USERNAME")
private String username;
My JUnit test:
#Test
public void testGetWebProfileByUsername() {
TestWebProfile wp = sso.getWebProfile("MARLENE");
System.out.println("name :" + wp.getFirstName());
System.out.println("last name :" + wp.getLastName());
}
My DAO implementation:
#Override
public TestWebProfile getWebProfile(String username) {
String sqlString = "select w from TestWebProfile w where w.username =:username";
return (TestWebProfile) getEntityManager()
.createQuery(sqlString, TestWebProfile.class)
.setParameter("username", username).getSingleResult();
}
After Googling for the past hour, the only culprit I found that seem to make sense was not having the #Id and #Column annotations, but I have those on the userNo variable. Any help that can be provided would be greatly appreciated!
Have a look at this. It gives some suggesstion for the same case http://java.dzone.com/tips/the-nasty-jpa-unknown-abstract
If you are using Java SE you have to add the fully qualified name of your entity class in persistence.xml, like so
<persistence-unit ...>
<class>your.custom.package.TestWebProfile</class>
</persistence-unit>
Omitting the package part may lead to your error.
Try adding inside your persistence-unit tag as follows:
<exclude-unlisted-classes>false</exclude-unlisted-classes>
For some reason in my case although I was listing all the classes inside my persistence unit using fully qualified names, eclipseLink didn't recognize them. Once added that line, it just works.
I was using:
Java SE 8
Maven Project
EclipseLink (Local not an application server)
Netbeans

how to access to subproperties with jpa metamodel in where clause

I have a two entities with relation between they are.
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private Integer id;
#NotNull
#Size(min = 3, max = 25)
private String firstName;
#NotNull
#Size(min = 3, max = 25)
private String lastName;
private String login;
private String password;
#OneToMany(mappedBy = "client")
private List<Project> projects;
}
and
public class Project implements Serializable {
private static final long serialVersionUID = 4762714047114442539L;
#Id
#GeneratedValue
private Integer id;
private String name;
#Temporal(TemporalType.TIMESTAMP)
private Date startDate;
#ManyToOne
#JoinColumn
private Client client;
}
I want to made a query using jpametamodel and Criteria API. Like this:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Project> q = cb.createQuery(Project.class);
Root<Project> projects = q.from(Project.class);
q.where(cb.equal(projects.get(Project_.client), clientId));
Problem for me that i don't know how to get access to "id" property of Client in this string:
q.where(cb.equal(projects.get(Project_.client), clientId));
i want to get something like
q.where(cb.equal(projects.get("client.id"), clientId));
but with jpametamodel. It is possible? :)
Tried something like this?
projects.get(Project_.client).get(Client_.id);