Posts

Showing posts with the label jpa

look for json key's value in jsonb

Image
Clash Royale CLAN TAG #URR8PPP look for json key's value in jsonb i am using criteriabuilder of jpa and i want to look for a value of a json field in a jsonb column. the jsonb looks like this : [{"field1":"value1","field2":"value2"},{"field3":"value3","field4":"value4"}] i started creating my predicates but i got errors. predicates.add( builder.like( builder.function("JSON_EXTRACT", String.class, root1.get("jsonBColumn"), builder.literal(""value3"")), "%" + searchValue + "%")); the error i am getting is org.postgresql.util.PSQLException: ERROR: function json_extract(jsonb, character varying) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. Can anybody help ? Here's an idea. Format your post so that it is hal...

Spring JPA getting a Projection from an Entity with a Query

Image
Clash Royale CLAN TAG #URR8PPP Spring JPA getting a Projection from an Entity with a Query In my application I have a Hero Entity. I also want to be able to return a list of each hero id and name. I got it working with this: @Repository public interface HeroEntityRepository extends JpaRepository<HeroEntity, Long> { @Query("select s.id, s.name from HEROES s") List<Object> getIdAndName(); } // in controller: @GetMapping public List<Object> getHeroNames() { return heroEntityRepository.getIdAndName(); } I tried the suggestion in another post to substitute the Object with a Interface, but then I receive a list of null values ( [{"name":null,"id":null},{"name":null,"id":null}, // etc ). The custom Interface: public interface HeroNameAndId { Long getId(); String getName(); } When making a Entity with only id and name values I received a 'ConverterNotFoundException'. Im not sure what the right way to go is....

Criteria API query fails

Image
Clash Royale CLAN TAG #URR8PPP Criteria API query fails I'm new at Criteria and can not understand the next thing. I've got entities with сonnection between each others User: //...fields... @OneToOne(fetch = FetchType.LAZY, mappedBy = "user") private Document document; Document: //...fields... @OneToOne(fetch = FetchType.LAZY) @PrimaryKeyJoinColumn private User user; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "doc_type_id", nullable = false) private DocType docType; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "citizenship_id", nullable = false) private Country country; DocType: //...fields... @Column(nullable = false, length = 5) private String code; Country: //...fields... @Column(nullable = false, length = 5) private String code; I need to find user with a document that contains the docCode (DocType.code) and citizenshipCode (Country.code). Dao: //private final EntityManager em; public List<User> filterUser(U...

One to many table relationship

Image
Clash Royale CLAN TAG #URR8PPP One to many table relationship I want to implement one to many table relationship with JPA: Main table Merchants @Entity @Table public class Merchants { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", updatable = false, nullable = false) private int id; @Column private String name; @Column private String login; } Second table Terminals which holds merchant id @Entity @Table public class Terminals { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", updatable = false, nullable = false) private int id; @Column private int merchant_id; @Column private String mode; } I tried this code implementation: @Entity @Table public class Merchants { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", updatable = false, nullable = false) private int id; @Column private String name; @...

JPA - Join another entity by EmbeddedId with 2 columns

Image
Clash Royale CLAN TAG #URR8PPP JPA - Join another entity by EmbeddedId with 2 columns Basically. I have a ProductEntity, like this: @Entity @Table(name = "product", schema = "shop") public class ShopProductEntity { @EmbeddedId private ProductEntityId id; @Column(name = "product_name") private String name; @Column(name = "product_category_id") private int categoryId; @Column(name = "product_language_id") private int languageId; // TODO: Get category by categoryId and languageId. private CategoryEntity category; I have another CategoryIdentity: @Entity @Table(name = "category", schema = "shop") public class CategoryEntity { @EmbeddedId private CategoryEntityId id; @Column(name = "category_name") private String name; @Column(name = "category_url") private String url; It has an EmbeddedId like this: @Embeddable public class CategoryE...

How to retrieve mapping table name for an entity in JPA at runtime?

Image
Clash Royale CLAN TAG #URR8PPP How to retrieve mapping table name for an entity in JPA at runtime? Is it possible to determine the native table name of an entity? If a Table annotation is present it's easy: Table entityClass.getAnnotation(Table.class).name() But what about if no Table annotation is present? Table Hibernate provides this information via the Configuration class: Configuration configuration.getClassMapping(entityClass.getSimpleName()).getTable().getName() Is there something similar in JPA? as far as i know this is indeed not part of the standard API, so you will have to rely on the actual implementation (hibernate, toplink, ...) to get what you want – Stefan De Boey Feb 26 '10 at 14:40 5 Answers 5 If no table annotation is presen...

More problems with JPA deletion of row in database

More problems with JPA deletion of row in database I have the following code that defines the relationship between three tables. public class Attachment implements Serializable { @Id @Column(name="attachment_id") private int attachmentId; @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="reference_id") private Reference reference; @OneToMany(mappedBy="attachment") private List<Reference> references; MORE STUFF; } public class Uuid implements Serializable { @Id @Column("name=uuid_id") @GeneratedValue(strategy=GenerationType.IDENTITY) private int uuidId; @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="reference_id") private Reference reference; MORE STUFF } public class Reference implements Serializable { @Id @Column(name="reference_id") @GeneratedValue(strategy=GenerationType.IDENTITY) private int referenceId; @OneToMany(mappedBy="reference") private List<Attachment> attachments; ...