I have the following field:
Entity
#Lob
#Column(name = "schema_ERD")
private String schemaERD;
DBeaver (oracle)
How can i force that entity to genere NCLOB instead of CLOB?
Solution:
#Nationalized
#Lob
#Column(name = "schema_ERD")
private String schemaERD;
Related
In My entity class "asset_docs" i am using photos_byte as column name defined in postgress sql defined as "photos_byte" as bytea . But whenever i am trying to use JPA framework to save someobject it throws the below error :
o.h.e.j.s.SqlExceptionHelper : ERROR: column "photos_byte" of relation "asset_docs" does not exist
#Id
#Basic(optional = false)
#Column(name = "id")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#Column(name = "created_at")
#Temporal(TemporalType.DATE)
private Date createdAt;
#Lob
#Column(name="photos_byte", columnDefinition="bytea")
private byte[] photosByte;
Whats wrong here ?
Im trying to do a simple search for two columns for a table.
My customer entity class snipet:
#Entity
#Table(name = "CUSTOMER")
public class Customer {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "CUSTOMER_ID", nullable = false)
private int customer_id;
#Column(name = "FIRST_NAME", nullable = false)
private String first_name;
#Column(name = "LAST_NAME", nullable = false)
private String last_name;
#Column(name = "PHONE_NUMBER", nullable = false)
private String phone_number;
#Column(name = "EMAIL")
private String email;
my customerRepository class snipet:
#Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
List<Customer> findAllByFirst_nameAndLast_name(String firstName, String lastname);
}
When compiling and running springBoot i get this error:
nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.owl.owlserver.repositories.CustomerRepository.findAllByFirst_nameAndLast_name(java.lang.String,java.lang.String)! No property first found for type Customer!
So its not able to detect the fields in the customer object, should I be importing the customer class into the repository somehow?
The problem lies in your snake_case naming convention, it's recommended to use camelCase naming convention.
As the Spring docs say:
Because we treat the underscore character as a reserved character, we
strongly advise following standard Java naming conventions (that is,
not using underscores in property names but using camel case instead).
You can see a related JIRA issue here -- underscores are not supported in property names.
I would recommend keeping the properties in a camel-case as per standards
Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "CUSTOMER_ID", nullable = false)
private int customerId;
#Column(name = "FIRST_NAME", nullable = false)
private String firstName;
#Column(name = "LAST_NAME", nullable = false)
private String lastName;
#Column(name = "PHONE_NUMBER", nullable = false)
private String phoneNumber;
#Column(name = "EMAIL")
private String email;
And then query as well as per Property Expressions
List<Customer> findAllByFirstNameAndLastName(String firstName, String lastname);
The whole point of Spring JPA is to simplify query building using the methods defined in Repository. The convention is to use the camelCase for naming field in Entity and the same while writing query methods. Just change the naming of entity fields from snake case to camel case and you're good to go.
I have an entity like this below
#Entity
public class Example
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "pl_id")
private Long id;
#Column(name = "pl_date")
#Convert(converter = DateConverterFromString.class)
private LocalDateTime date;
#Column(name = "pl_number")
private String number;
#Column(name = "pl_type")
private Long result;
#Column(name = "pl_package_id")
private Long packageId;
// getters&setters
}
I'm trying to get the number of unique pl_number results for a given type.
Like, I'm trying to make hibernate run this query for me
select count(distinct pl_number) from sample_table where pl_type=200;
What I have tried so far are Spring Data Query Methods and JPQL or native queries.
I did
countDistinctNumberByResult(Long result);
and I also tried to write a native and jpql query for this. But what it returns is a query like below
select
distinct count(distinct provisionl0_.pl_id) as col_0_0_
from
sample_table provisionl0_
where
provisionl0_.pl_type=?
and (
provisionl0_.pl_date between ? and ?
)
Anyone has a fix or a better idea?
Thanks.
When I used Spring datajpa and Hibenate to read Oracle, I met an error, I'll desc this:
The Oracle table name includes special character(double quotes), so, I have to use SELECT * FROM "Graph" WHERE "ID"=1 .
So, when I use hibernate to write a entity class, I have to point its special name.
Here is the ddl which is used to create "Graph" table:
CREATE TABLE "ATLASCOPCO_TOOLSNET"."Graph" (
"ID" NUMBER(19) NOT NULL ,
"ResultID" NUMBER(19) NOT NULL ,
"GraphTypeID" NUMBER(19) NOT NULL ,
"SampleTime" BINARY_FLOAT NOT NULL ,
"AngleOffset" BINARY_FLOAT NOT NULL ,
"GraphValues" BLOB NOT NULL ,
"AngleFactor" BINARY_DOUBLE NULL ,
"TorqueFactor" BINARY_DOUBLE NULL ,
"StartTime" BINARY_FLOAT NULL ,
"EndTime" BINARY_FLOAT NULL
)
LOGGING
NOCOMPRESS
NOCACHE
When I used the normal table name like this:
#Entity
#Table(name = "Graph")
#Getter
#Setter
public class Graph {
#Id
#Column(name = "ID\"")
private Long id;
#Column(name = "ResultID")
private Long resultId;
#Column(name = "GraphTypeID")
private Long graphTypeID;
#Column(name = "SampleTime")
private BINARY_FLOAT sampleTime;
#Column(name = "AngleOffset")
private BINARY_FLOAT angleOffset;
#Column(name = "GraphValue")
private byte[] graphValue;
#Column(name = "AngleFactor")
private BINARY_DOUBLE angleFactor;
#Column(name = "TorqueFactor")
private BINARY_DOUBLE torqueFactor;
#Column(name = "StartTime")
private BINARY_FLOAT startTime;
#Column(name = "EndTime")
private BINARY_FLOAT endTime;
}
I got error:Caused by: java.sql.SQLSyntaxErrorException: ORA-00972: identifier is too long
When I used this:
#Entity
#Table(name = "\"Graph\"")
#Getter
#Setter
public class Graph {
#Id
#Column(name = "\"ID\"\"")
private Long id;
#Column(name = "\"ResultID\"")
private Long resultId;
#Column(name = "\"GraphTypeID\"")
private Long graphTypeID;
#Column(name = "\"SampleTime\"")
private BINARY_FLOAT sampleTime;
#Column(name = "\"AngleOffset\"")
private BINARY_FLOAT angleOffset;
#Column(name = "\"GraphValue\"")
private byte[] graphValue;
#Column(name = "\"AngleFactor\"")
private BINARY_DOUBLE angleFactor;
#Column(name = "\"TorqueFactor\"")
private BINARY_DOUBLE torqueFactor;
#Column(name = "\"StartTime\"")
private BINARY_FLOAT startTime;
#Column(name = "\"EndTime\"")
private BINARY_FLOAT endTime;
}
I got Caused by: java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
Remove the special character \" in column names, it is causing the issue. JPA and Oracle driver will handle all correctly.
You can omit the ", ANSI SQL does work too.
CREATE TABLE NEW_TABLE (ID NUMBER(19) NOT NULL , NAME VARCHAR2(19) NOT NULL);
Let's Say you have a Column in a table whose name is "id" as shown below:
Then to use the special character (") you need to add an escape sequence as below:
#Column(name="`\"id\"`")
private int id;
The result will be a column with name as ("id").
Please find the below updated Graph POJO class:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table (name="`\"Graph\"`")
#Getter
#Setter
public class Graph {
#Id
#Column (name="`\"ID\"`")
private Long id;
#Column (name="`\"ResultID\"`")
private Long resultId;
#Column(name="`\"GraphTypeID\"`")
private Long graphTypeID;
#Column (name="`\"SampleTime\"`")
private BINARY_FLOAT sampleTime;
#Column (name="`\"AngleOffset\"`")
private BINARY_FLOAT angleOffset;
#Column (name="`\"GraphValue\"`")
private byte[] graphValue;
#Column (name="`\"AngleFactor\"`")
private BINARY_DOUBLE angleFactor;
#Column (name="`\"TorqueFactor\"`")
private BINARY_DOUBLE torqueFactor;
#Column (name="`\"StartTime\"`")
private BINARY_FLOAT startTime;
#Column (name="`\"EndTime\"`")
private BINARY_FLOAT endTime;
}
To retrieve the data from your Graph table use below code in Hibernate:
Query query=session.createSQLQuery("SELECT * FROM `\" Graph \"` WHERE `\"ID\"`=1 ");
Hope this helps you.
How can I use a character in JPA's IN Clause in JPQL?
For Example In oracle DB I have this Query:
select * from channel o where channel_mode IN ('O','R')
Channel mode is a char column in DB.
I get a error from JPQL that invalid IN argument[o], it is a character.
#Chris
This is the JPQL:
typeMasterList = em.createQuery("select object(o) from ChannelRequest as o where o.crqState IN ('O','R') and o.crqMode = 'B' ").getResultList();
And the Entity ChannelRequest is:
#Entity
#Table(name = "CHANNEL_REQUEST")
#XmlRootElement
public class ChannelRequest implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#NotNull
#Column(name = "CRQ_ID")
private Long crqId;
#Size(max = 100)
#Column(name = "CRQ_QUEUE")
private String crqQueue;
#Size(max = 160)
#Column(name = "CRQ_DATA")
private String crqData;
#Column(name = "CRQ_STATE")
private Character crqState;
#Column(name = "ACTIVE_FLAG")
private Short activeFlag;
#Column(name = "ENABLED")
private Short enabled;
#Size(max = 100)
#Column(name = "CRQ_ADDRESS")
private String crqAddress;
#Column(name = "CRQ_MODE")
private Character crqMode;
#Column(name = "DCO")
#Temporal(TemporalType.TIMESTAMP)
private Date dco;
#Column(name = "DLUO")
#Temporal(TemporalType.TIMESTAMP)
private Date dluo;
#OneToMany(mappedBy = "rpmrqCrId")
private Collection<RpmRequest> rpmRequestCollection;
#JoinColumn(name = "MM_ID", referencedColumnName = "MM_ID")
#ManyToOne
private MediumMaster mmId;
#JoinColumn(name = "MC_ID", referencedColumnName = "MC_ID")
#ManyToOne
private MediumChannel mcId;`enter code here`
This is the error I get:
invalid IN expression argument [O], expected argument of type [java.lang.Character]
The JPA specification states that a string literal is enclosed in single quotes (e.g. 'X' is a string of length 1 not the character X).
It looks like in your case, JPQL is expecting a list of characters, not strings. You can work around the issue by passing in the character list as a parameter instead:
typeMasterList = em.createQuery("... o.crqState IN :states ...")
.setParameter("states", Arrays.asList('O', 'R'))
.getResultList();
Side note: you could make your life a lot easier by using an enum instead of characters. You could then reference the enum values directly in the query without issue. At the database-level, the enums could still map to a single character.
Did you try using a string instead of a char, i.e. "O", "R".