Last active
December 11, 2015 03:29
-
-
Save wombat/4538576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Product implements Serializable { | |
@Id | |
private Long id; | |
@Transient | |
private String translation; | |
public String getTranslation() { | |
return translation; | |
} | |
public void setTranslation(String translation) { | |
this.translation = translation; | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class ProductDescription implements Serializable { | |
@EmbeddedId | |
ProductDescriptionPK id; | |
private String translation; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Embeddable | |
public class ProductDescriptionPK implements Serializable { | |
@Column(nullable=false) | |
private String locale; | |
@ManyToOne(optional = false) | |
private Product product; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Stateless | |
public class ProductDescriptionPK implements Serializable { | |
@PersistenceContext | |
private EntityManager em; | |
public String findProductTranslation(Product product, Locale locale) { | |
return em.createQuery("SELECT pd.translation FROM ProductDescription pd WHERE pd.id.locale = :locale AND pd.id.product = :product", String.class).setParameter("locale", locale.toString()).setParameter("product", product).getSingleResult(); | |
} | |
public List<Product> findAllProducts(Locale locale) { | |
Query q = em.createQuery("SELECT p, pd.translation FROM ProductDescription pd JOIN pd.id.product p WHERE pd.id.locale = :locale ORDER BY pd.translation ASC", Object[].class); | |
q.setParameter("locale", locale.toString()); | |
List<Object[]> results = q.getResultList(); | |
List<Product> products = new ArrayList<Product>(); | |
for(Object[] result : results) { | |
Product product = (Product) result[0]; | |
String translation = (String) result[1]; | |
product.setTranslation(translation); | |
products.add(product); | |
} | |
return products; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment