My JPA entity has a UUID attribute, which is stored as char(36) in mysql. When I query data i'm receving 66323735-3039-6262-2d31-3764392d3466 instead of f27509bb-17d9-4f37-b336-8603f2d34394. When I enabled hibernate logs, I could see
org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([col_1_0_] : [BINARY]) - [66323735-3039-6262-2d31-3764392d3466]
extracted value is [BINARY] instead of [VARCHAR] like other attributes.
Any clue as to why this is happening?
Also,
I've tried to run hibernate generated query on mysql and that returns correct results.
Other column values including UUIDs are being returned correctly.
I'm using an interface based entity projection to retrieve limited data and not the whole entity.
edit: I just added trim(colName) and now it's returning correct UUID value. But still not sure of the issue at hand.
Do this for hibernate 6. It will generate character type (for mysql char(36)).
#org.hibernate.annotations.JdbcTypeCode(SqlTypes.CHAR)
protected UUID id;
Related
I'm having a use case here which I can't seem to solve. Basically, I need to create a webservice where users may query the couchbase cluster "dynamically". Indeed, i'm storing metadata of different files, and the "creation" of this metadata is up to the user : I don't have specific fields in my Java POJO, i'm inserting a MAP which gets inserted as a nested object in couchbase.
Now the query I need is pretty simple on paper and looks something like this :
#Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND $1 = $2")
List<FileMetadata> findListMetadata(String pKey, String pValue);
But it doesn't seem to work, $1 doesn't seem to ever get replaced by the pKey variable.
I'm using CouchBase 4.5 with the Spring Data connector.
Any ideas on how to solve that use case ?
You need to dynamically generate the query string, so that pKey is inserted into the query string and pValue is passed as a parameter (as you are doing).
If I query for a mediumint column from a MySQL table through jdbc, what type would I get back in the ResultSet object?
Or would that be an error?
(I know JDBC's Types.java doesn't have MEDIUMINT).
You can use almost any Java Type (look here) but the best way is to use Integer, since Short would be to short for MEDIUMINT and to get back the result you should use the method rs.getInteger("COLUMNNAME")
Not an error. The column type as returned by ResultSetMetaData.getColumnType() is 4, INTEGER. If you invoke ResultSet.getObject(), you'll get a java.lang.Integer. Invoking getInt() or getLong() will both work just fine.
The type returned by the JDBC driver, if not represented as part of the JDBC specification itself (the infamous table in Appendix B), will be the smallest Java type that can represent the full range of values of the MySQL type.
I am trying to use DataNucleus's JDO in my own Java project (not GAE), and i need to store quite long pieces of text in my DB.
I am using Eclipse to do all the dirty work, like enhancing and creating the schema, but unfortunately, all String ivars are mapped to VARCHAR columns. But how do i get Datanucleus to create a LONGTEXT column for me?
I also tried the #Colunn annotation, but i still got the VARCHAR.
#Persistent
#Column(name="COMPONENT", jdbcType="LONGVARCHAR", length=1000000)
private String component;
I also tried specifying LONGTEXT as jdbcType, but the Schematool informed me, that that datatype could not be used.
Thank you in advance,
Happy New Year,
Best regards,
Timofey.
LONGTEXT is not a JDBC type. They are all clearly listed in http://docs.oracle.com/javase/6/docs/api/java/sql/Types.html
The DataNucleus log tells you what JDBC types are available for that JDBC driver (since it provides that information to the utilising software). It chooses to map JDBC type LONGVARCHAR onto "LONG VARCHAR" IIRC (easily visible by using SchemaTool "dbinfo"). You could obviously generate the schema into a text file and update it yourself before applying it.
#Persistent
#Column(name="COMPONENT", jdbcType="CLOB")
private String component;
The data type CLOB maps to medium text. But once the database is created you can change it to LONGTEXT
I'm transferring a MySQL table to MongoDB. There is a primary key id in my MySQL table and I want this id to be converted to the _id in MongoDB.
I use php's MongoCollection::insert( $mysql_array );
However it doesn't work because if I set $mysql_array['_id'] it is seen by mongo as a String instead of a MongoId. I tried $mysql_array['_id'] = new MongoId( $id ) but it doesn't allow me to override the default _id value. I saw that all my MySQL's integer columns are converted to string by MongoCollection::insert(). If I could set MongoCollection::insert() to correctly transfer an integer it would maybe work.
typecast the _id to a integer value like this...
(int) $mysql_array['_id']
You'll find yourself doing this a lot in mongoDB
The ObjectId is a special type in Mongo, but the _id property doesn't have to be of this type. You can't coerce a string or number into an ObjectId, and you shouldn't.
I assume the problem as you perceive it is that your insert worked, but when you looked at the data in the database the _id property didn't look like _id: ObjectId("1234") (if the original ID was 1234). This is as it should be, and it's perfectly fine.
The idea with ObjectId is that it has a predefined structure that makes it guaranteed (mostly) to be unique across a Mongo cluster, but this also means that it has to have this structure, otherwise it is not an ObjectId.
You also mention that all your integer columns are converted to strings. PHP and PHP libraries, are notoriously sloppy when it comes to types, so make sure that it's not the case that the values are already strings when they come from the MySQL result set. Worst case you have to explicitly cast the values before inserting them into Mongo.
You won't be able to convert an arbitrary String value into an Mongo ObjectId due to its specific characteristics (12 bytes -> 24 chars generated from 4 bytes timestamp, 3 bytes client hostname, 2 bytes PID, 3 bytes inc value).
Either you abandon using the MongoId type in your collection's _id-fields and use your MySQL-ID as a string instead (which is not a problem and makes the most sense) or you let Mongo to generate the documents' _id for you, which is also a suitable solution if you want to be able to use the MongoId functions (assuming you're working with PHP):
The MongoId class
Choosing the second solution you still are able to store your MySQL-IDs in another field of the doc, like id or *mysql_id* to reference them later.
Concerning your question about (int) and (string) values: Are you sure they come as a PHP integer from your MySQL DB? If so, they usually should be stored as integers in Mongo. Check it with a var_dump() and in case of incompatibility cast it with an (int). Maybe it would be helpful if you post your select/insert code...
Use MongoCollection::save() and your array should work.
Here's my scenario. I save a bunch of Strings containing asian characters in MySQL using Hibernate. These strings are written in varbinary columns. Everything works fine during the saving operation. The DB contains the correct values (sequence of bytes). If I query (again using Hibernate) for the Strings that I saved I get the correct results. But when Hibernate fills the entity to which the Strings belong with the values from the DB I get different values then the ones I used in the query that retrieved them. Instead of receiving the correct values I receive a bunch of FFFD replacement characters.
For example: if I store "하늘" in the DB and then I query for it, the resulting String will be \uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD.
the DB connection has the following parameters set useUnicode=true&characterEncoding=UTF-8,
I've tried using the following configurations for Hibernate but that didn't solve the problem:
- connection.useUnicode = true
- connection.characterEncoding = UTF-8
By the way, this all works fine if the MySQL columns are of type varchar.
What am I missing? Any suggestions?
Thanks
Set the connection character set to be binary too:
SET NAMES 'binary';