我需要以编程方式在数据存储区中生成序列,但需要能够检测它们的存在而不是在它们已经存在的情况下创建.有人知道提取此信息所需的JDBC元数据吗?
对DatabaseMetadata进行粗略扫描并未发现合适的方法; 我可以获取所有表/视图和关联的键/索引等,但不能获取该模式的序列.有没有人知道一种方法,最好是数据库独立的,但如果没有,那么对于尽可能多的数据库(想想oracle有一个user_sequence表?但这只是一个数据库,我需要支持其他数据库).
提前致谢
我已经开始使用Hibernate 4.3.8并尝试插入和检索用户的基本程序.以下是我的文件.
的hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
Run Code Online (Sandbox Code Playgroud)
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mywork</property>
<property name="connection.username">****</property>
<property name="connection.password">*****</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_structured_entries">true</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.parvez.hibernate.model.User"/>
</session-factory>
Run Code Online (Sandbox Code Playgroud)
Java代码HB2Test.java
package com.parvez.hibernate.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; …Run Code Online (Sandbox Code Playgroud) 在我当前的实现中,我为每个db表都有单独的实体类.我正在使用JPA和eclipselink-2.5.2.这对我来说很好,但在某些时候,当数据很大时,它会滞后.这就是我决定开始使用@ Embedded,@ Embeddable和@EmbeddedId的原因.在这样做时,我收到的错误对我来说非常奇怪.这是完整的堆栈跟踪:https://gist.githubusercontent.com/tjDudhatra/b955812e0d1a71cf97f1/raw/11ea458869e24baae744530417ac99bc877ed514/gistfile1.txt
具体,让我给你一个确切的场景,在这种情况下,我得到了例外.考虑这个有三个类的Code块.一个注释为@Entity,其他两个注释为@Embeddable.我知道在一个类中我们不能定义@Id和@EmbeddedId而我没有这样做,那么在部署服务器时,我得到的异常只表示:
[class org.apache.{SomeClass}]同时包含@EmbdeddedId(在属性[id]上)和@Id(在属性[]上.两种ID类型都不能在同一实体上指定.
@Entity
@Table(name="user")
public class User {
@ID
public Long id;
@Column(name="userCode")
public String userCode;
@ElementCollection
@CollectionTable(name = "address", joinColumns = @JoinColumn(name = "user_id"))
public List<Address> addressList;
....
}
@Embeddable
public class Address {
@EmbeddedId
@Column(name = "id")
public Long id;
@Column(name="userId")
public Long userId;
@Column(name="address-line-1")
public String addressLine1;
@Column(name="address-line-2")
public String addressLine2;
@ElementCollection
@CollectionTable(name = "phone", joinColumns = @JoinColumn(name = "user_id"))
protected List<Phone> phoneList;
....
}
@Embeddable
public class Phone { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Spring Data JPA 与 MyBatis 一起使用。由于 MyBatis 没有 Vendor Adapter,这里有什么替代方案?
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.abc.xyz.domain"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
当我尝试初始化我的应用程序时,出现以下异常。
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No PersistenceProvider specified in EntityManagerFactory configuration, and chosen PersistenceUnitInfo does not specify a provider class name either
Run Code Online (Sandbox Code Playgroud)
谢谢
最近,我开始使用Spring Boot进行Web应用程序开发.
这是我的.properties文件内容:
#data source configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/sampledb
spring.datasource.schema=sample
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.minimumIdle=3
spring.datasource.maximumPoolSize=5
#jpa properties configuration
#spring.jpa.show-sql=false
spring.jpa.databasePlatform=org.hibernate.dialect.PostgreSQL82Dialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=validate
#spring.jpa.properties.hibernate.default_schema=sample
Run Code Online (Sandbox Code Playgroud)
我实体类的这一部分:
@Entity
@Table(name = "sample_info")
public class SampleInfo implements Serializable{
private Long id;
private String code;
private Long serialNumber;
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "sample_info_seq_gen"
)
@SequenceGenerator(
name = "sample_info_seq_gen",
sequenceName = "sample_info_seq",
allocationSize = 1
)
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
} …Run Code Online (Sandbox Code Playgroud) postgresql hibernate spring-data spring-data-jpa spring-boot
我有这个例外:
org.hibernate.MappingException:未知的持久化类:java.lang.Long
我正在使用Hibernate 5.1.0.Final和spring-orm 4.2.4.RELEASE
我的spring配置文件(spring.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/ecollection" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="hibernateAnnotatedSessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.ecollection.model.Adresse</value>
<value>com.ecollection.model.Utilisateur</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="utilisateurDao" class="com.ecollection.dao.UtilisateurDaoImpl">
<property name="sessionFactory" ref="hibernateAnnotatedSessionFactory" />
</bean>
<bean id="adresseDao" class="com.ecollection.dao.AdresseDaoImpl"> …Run Code Online (Sandbox Code Playgroud) 我正在研究一个实体库.我在我的实体上添加了一些bean验证注释.
我坚信在类路径上有一个bean验证实现.
@javax.validation.constraints.NotNull工作,@javax.validation.constraints.AssertTrue不起作用.
class MyEntity {
@AssertTrue // does't work
public boolean hey() {
return false;
}
@NotNull // works; violation while persist
private String some;
}
Run Code Online (Sandbox Code Playgroud)
我可能做错了什么?
我使用org.hibernate:hibernate-validator和更改它org.apache.bval:bval-jsr没有任何区别.
UPDATE
实际上是调用该方法.我检查日志.
这是我的方法.
@AssertTrue(message = "a property must be eclusively system or owned")
private boolean execlusivelySystemOrOwned() {
logger.info("execlusivelySystemOrOwnded()");
final boolean result = system ^ (getOwner() != null);
logger.log(Level.INFO, "result: {0}", result);
return result;
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个 spring mvc 项目,我想转换我在我在 spring mvc 3 上工作时编写的 applicationContext.xml 中的 jpa 配置,现在我想转移到 spring Mvc 4 并编写我所有的 Jpa仅使用 Java 注释的配置有人可以帮助我
这是我的 applicationContext 文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/db_adpub"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="datasource"></property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
<property name="persistenceUnitName" value="adpub"></property>
</bean>
<bean id="transactionManager" …Run Code Online (Sandbox Code Playgroud) 我有一个有关的问题javax.validation.constraints.NotNull annotation。在我的项目中,我确实有如下所示的类树:
@Inheritance(strategy = InheritanceType.JOINED)
class Ssss {
@ManyToOne
private Xxxx x;
public Xxxx getXxxx() {
return x;
}
}
@Inheritance(strategy = InheritanceType.JOINED)
class Yyyy extends Ssss {
@Override
//some not important annotations
public Xxxx getXxxx() {
return super.getXxxx();
}
}
@Inheritance(strategy = InheritanceType.JOINED)
class Zzzz extends Ssss {
@Override
//some not important annotations
@NotNull
public Xxxx getXxxx() {
return super.getXxxx();
}
}
Run Code Online (Sandbox Code Playgroud)
这三个类作为三个表存储在数据库中。对于架构创建,我正在使用Hibernate:
hibernate.hbm2ddl.auto=create
Run Code Online (Sandbox Code Playgroud)
它是一个预期的行为,Hibernate对增加NOT NULL对xxxx_object_id存储在父类生成的表场SSSS类似以下内容:??
我找不到有关休眠如何在继承的getter上使用@NotNull的任何相关信息。
有人可以帮我解决这个问题吗?
最好的祝福。 …
我刚刚开始使用 Querydsl,并且非常享受它为与 JPA 交互带来的改进。
我剩下的一个问题是为什么你必须用 包装like参数%?
我是否配置不当?考虑到有一种方法starts/endsWith,我假设like(str)会自动包装我的str.
但令我惊讶的是,我需要这样做:"%" + str + "%"得到我所期望的。
这是为什么?有没有更好的办法?
java ×9
hibernate ×4
spring ×3
jpa ×2
postgresql ×2
spring-data ×2
eclipselink ×1
embeddable ×1
jdbc ×1
jpql ×1
mybatis ×1
querydsl ×1
sequence ×1
spring-boot ×1
spring-mvc ×1
sql ×1