Showing posts with label database数据库. Show all posts
Showing posts with label database数据库. Show all posts

2022-07-06

Resolved - @PreUpdate not working in JUnit test.

 



javax.persistence.PreUpdate





object need to be "dirty" to trigger...


obj.setColumn01("text");
repo.save(obj);


then make obj "dirty" before save to trigger @PreUpdate


obj.setColumn01("new text);
repo.save(obj);




Solution

As you said, the callback method is called the second time, if the entity is detached or fetched again from DB.

I cannot explain it exactly, but can think of the scenario described here, when no dirty fields are identified before the second save() call and thus the @PreUpdate callback not called. Or it may be simply a bug within your version of EclipseLink.

2021-04-16

Cannot access locahost:8080/h2-console for H2Database?


Hint: 






Check if you set a base path in application.properties.

For example, if you have a setting

server.contextPath=/api

You access the h2 Console under

http://localhost:8080/api/h2-console

Obvious, but that was it for me









2020-12-29

Spring JPA

 


怀疑问的发现:


Seems able to join another entity(table) by using reference below:

  1. SELECT P.C.attribute FROM Parent AS P;
  2. SELECT PCattribute FROM Parent AS P;





2019-09-03

datasource c3p0 vs org.apache.commons.dbcp.BasicDataSource




https://stackoverflow.com/questions/520585/connection-pooling-options-with-jdbc-dbcp-vs-c3p0
DBCP is out of date and not production grade. Some time back we conducted an in-house analysis of the two, creating a test fixture which generated load and concurrency against the two to assess their suitability under real life conditions.

DBCP consistently generated exceptions into our test application and struggled to reach levels of performance which C3P0 was more than capable of handling without any exceptions.

C3P0 also robustly handled DB disconnects and transparent reconnects on resume whereas DBCP never recovered connections if the link was taken out from beneath it. Worse still DBCP was returning Connection objects to the application for which the underlying transport had broken.

Since then we have used C3P0 in 4 major heavy-load consumer web apps and have never looked back.

UPDATE: It turns out that after many years of sitting on a shelf, the Apache Commons folk have taken DBCP out of dormancy and it is now, once again, an actively developed project. Thus my original post may be out of date.

That being said, I haven't yet experienced this new upgraded library's performance, nor heard of it being de-facto in any recent app framework, yet.










Hibernate ORM 5.3.11.Final = current stable release


As of 2019-Sep-03_pm021556
Hibernate ORM 5.3.11.Final  = current stable release


Hibernate ORM 5.3.11.Final User Guide.html

https://docs.jboss.org/hibernate/stable/core/userguide/html_single/Hibernate_User_Guide.html#_system_requirements

System Requirements

Hibernate 5.2 and later versions require at least Java 1.8 and JDBC 4.2.
Hibernate 5.1 and older versions require at least Java 1.6 and JDBC 4.0.




Relevant:
https://spring.io/blog/2018/09/21/spring-framework-5-1-goes-ga

Spring Framework 5.1 requires JDK 8 or higher and specifically supports JDK 11 as the next long-term support release. It comes with initial refinements for GraalVM compatibility and deeply integrates with the recently released Reactor Californium and Hibernate ORM 5.3.







2019-08-28

No PersistenceProvider - Caused by: java.lang.IllegalArgumentException: No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either











Change the emf bean configuration and add a new bean called jpaVendorAdapter:
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf">
  <property name="packagesToScan" value="com.medsoft.stadto.entity" />
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
  <property name="persistenceUnitName" value="stadto"/>
  <property name="jpaProperties">
    <props>
      <prop key="hibernate.show_sql">true</prop>
      <prop key="hibernate.hbm2ddl.auto">create</prop>
    </props>
  </property>
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  <property name="showSql" value="true"/>
  <property name="generateDdl" value="true"/>
  <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
</bean>
Also make sure you have a persistence.xml in the META-INF directory:
<?xml version="1.0"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  version="2.1">
  <persistence-unit name="stadto">
    //No need to specify the provider as we already have specified JPA vendor in applicationContext.xml
  </persistence-unit>
</persistence>






To remove persistence.xml, refer to: https://billson.blogspot.com/2019/08/caused-by-javalangillegalstateexception.html







Caused by: java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}






Reference URL: https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa


5. Going Full XML-less

Usually, JPA defines a persistence unit through the META-INF/persistence.xml file. Starting with Spring 3.1, the persistence.xml is no longer necessary. The LocalContainerEntityManagerFactoryBean now supports a ‘packagesToScan' property where the packages to scan for @Entity classes can be specified.
This file was the last piece of XML we need to remove. We can now set up JPA fully with no XML.
We would usually specify JPA properties in the persistence.xml file. Alternatively, we can add the properties directly to the entity manager factory bean:
factoryBean.setJpaProperties(this.additionalProperties());
As a side-note, if Hibernate would be the persistence provider, then this would be the way to specify Hibernate specific properties.







add this into your servlet context.xml

<property name="packagesToScan" value="com.howtodoinjava.entity" />








2018-11-27

Check mysql access privileges.



resource URL: https://dev.mysql.com/doc/refman/5.5/en/privilege-system.html



SHOW GRANTS FOR user_abc;


2018-04-26

Jackson ObjectMapper writeValueAsString StackOverflowError



com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.vividsolutions.jts.geom.Point["envelope"]->           repeat 997 times           ->com.vividsolutions.jts.geom.Point["envelope"])



Working solution URL: https://github.com/bedatadriven/jackson-datatype-jts/issues/8





ObjectMapper om = new ObjectMapper();
om.registerModule( new com.bedatadriven.jackson.datatype.jts.JtsModule.JtsModule() );


2018-04-25

Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Cannot get geometry object from data you send to the GEOMETRY field


Environment:

JDK 1.7.0_80
Tomcat 8.0.36
MySql 5.5.50
Spring 4.3.16.RELEASE
Hibernate 4.3.11.Final

Hibernate Spatial 4.3


Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Cannot get geometry object from data you send to the GEOMETRY field

Getting fail to store Polygon Shape Object in Geometry Datatype with hibernate spatial API




Check column after insert:



SELECT AsText( geometry_column ) FROM table;

2014-07-05

MySql: Check each database size running in mysql.



Source URL: http://stackoverflow.com/questions/1733507/how-to-get-size-of-mysql-database



SELECT table_schema                                        "DB Name", 
   Round(Sum(data_length + index_length) / 1024 / 1024, 1) AS "DB Size in MB" 
FROM   information_schema.tables 
GROUP  BY table_schema
ORDER BY Round(Sum(data_length + index_length) / 1024 / 1024, 1) DESC;


SELECT table_schema                                        "DB Name", 
   Round(Sum(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" 
FROM   information_schema.tables 
GROUP  BY table_schema;











MySql Error code 1017



# mysql connected.
# able to run use database.
# cannot do query select statement.
# run CHECK TABLE, found: cannot find file frm errno 13
# cannot run REPAIR TABLE
# cannot dump databases using: mysqldump





Source URL: http://stackoverflow.com/questions/6443428/cant-find-file-ci-users-frm-errno-13

Example solution in URL above: chown mysql:mysql /var/lib/mysql/ci/*

go to mysql database data folder:

cd /opt/Program/mysql/data/yourdatabase/

chown mysql:mysql *






















2013-07-31

MySql Select statement export to a file.



SELECT column_a, column_b, column_c FROM tb_abc INTO OUTFILE 'C:/folders/location/tb_abc.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n';


2007-12-11

sql server functions

SQL Server 2000

There are three types of UDF in Microsoft SQL Server 2000:

  1. Scalar functions.
  2. Inline table-valued functions.
  3. Multistatement table-valued functions.

Scalar functions return a single data value (not a table) with RETURNS clause. Scalar functions can use all scalar data types, with exception of timestamp and user-defined data types. Inline table-valued functions return the result set of a single SELECT statement. Multistatement table-valued functions return a table, which was built with many TRANSACT-SQL statements.

User-defined functions can be invoked from a query like built-in functions such as OBJECT_ID, LEN, DATEDIFF, or can be executed through an EXECUTE statement like stored procedures.

2007-12-10

The Structure of Stored Procedures

The Structure of Stored Procedures
(from SQL SERVER 2000 Stored Procedure Programming)

We will pause aminute to explain the structure of a stored procedure.
A stored procedure encapsulates a relatively simple Select statement for later use.
It returns a recordset containing values from the Selected column.

The recordset will contain only records that matched with the WHERE statement.

The code of a stored procedure consists of a header and a body.

The header of the stored procedure defines external attributes of the stored procedure—its name and a list of one or more parameters.

A example stored procedure has two parameters.
Parameter names must start with the @ character.
The developer must also define a datatype for each parameter.
The header must begin with the Create Procedure keyword and finish with the As keyword.

The body of the stored procedure contains the Transact-SQL statements to be executed when the stored procedure runs.

In this example, there is just one Select statement using the procedure parameters.

Google Referrals