我正在将Hibernate和JPA用于一个小项目.
不知何故,当试图获得一个类型化的查询时,
java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerImpl.createQuery(Ljava/lang/String;Ljava/lang/Class;)Ljavax/persistence/TypedQuery
Run Code Online (Sandbox Code Playgroud)
被抛出; org.hibernate.ejb.EntityManagerImpl来自hibernate-entitymanager-3.3.2.GA.jar.
抛出上述异常是不行的:
public Account read(Account entity) {
EntityManager em = ManagedEntityManagerFactory.getEntityManager();
String jpql = JPQLGenerator.readAccount();
TypedQuery<Account> typedQuery =
em.createQuery(jpql, Account.class);
typedQuery.setParameter("accountId", entity.getAccountId());
return typedQuery.getSingleResult();
}
Run Code Online (Sandbox Code Playgroud)
但是这没关系:
public Account read(Account entity) {
EntityManager em = ManagedEntityManagerFactory.getEntityManager();
String jpql = JPQLGenerator.readAccount();
Query query =
em.createQuery(jpql);
query.setParameter("accountId", entity.getAccountId());
Account account = null;
Object obj = query.getSingleResult();
if(obj instanceof Account) {
account = (Account)obj;
}
return account;
}
Run Code Online (Sandbox Code Playgroud) 我知道如何在JTextField上添加文本侦听器,在文本更改时触发,并在操作时修改JTextField的文本.
我尝试使用addInputMethodListener它似乎是合适的,但它似乎不起作用.我也尝试过,textField.getDocument().addDocumentListener()但是java.lang.IllegalStateException: Attempt to mutate in notification当我尝试修改textField的文本时,这会抛出.
假设我有班级:
@Entity
public class Bean {
@Id
private String beanId;
//other fields & setters and getters
}
Run Code Online (Sandbox Code Playgroud)
以及相应的Spring Data JPA存储库,我希望在List<String>所有存储库中存在beanIds.
@RepositoryDefinition(domainClass = Bean.class, idClass = String.class)
public interface BeanRepository {
@Query("select b.beanId from Bean b")
List<String> findAllBeanId();
}
Run Code Online (Sandbox Code Playgroud)
如上所述,一切都按预期工作; 但这是一个简单的操作,我不想显式编写查询.该方法的名称应该是什么,以便Spring Data可以解析它并获得上述查询(或相同的功能).我在参考文档中搜索了两本关于Spring Data的书.上述域名(findAllBeanId)和其他人,我已经试过(findBeanId,findBeanBeanId等)抛出以下异常的根本原因:
org.springframework.data.mapping.PropertyReferenceException: No property find found for type Trade
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:72)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260) …Run Code Online (Sandbox Code Playgroud) 有一个使用 SQL DDL 脚本创建的表,其中有一列类型为_INT8。如果我尝试将它映射到long(即 Postgres INT8),它会在堆栈的末尾抛出。
Caused by: org.hibernate.HibernateException: Wrong column type in [schme_name].[table_name] for column [column_name]. Found: _int8, expected: int8
at org.hibernate.mapping.Table.validateColumns(Table.java:373)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1265)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:508)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:920)
Run Code Online (Sandbox Code Playgroud)
如果我尝试将它映射到long[](或任何其他数组类型)而不是Found: _int8, expected: bytea
如何_INT8使用 Hibernate 将Postgres映射到 Java 类型?
假设我有这个课程:
class MyClass {
int myInt
MyClass(myInt) {
this.myInt = myInt
}
def myMethod() {
print this.myInt
}
}
Run Code Online (Sandbox Code Playgroud)
在某处我有:
def myClass1 = new MyClass(1)
def myMethodClosure = myClass1.&myMethod
def myClass2 = new MyClass(2)
Run Code Online (Sandbox Code Playgroud)
现在,如果我把myMethodClosure()它会调用myMethod()上myClass1的实例,将打印1.我想是调用相同myMethodClosure,但在不同的情况下,在这种情况下,就myClass2这样就可以打印2,这可能吗?
我尝试过使用setDelegate(),但它不起作用.我也看到thisObject封闭类中有字段,但它没有setter,只有getter.
我的东西看起来像是类似的规格:
def "my spec"(Record record) {
given:
Something something = getSomething()
and:
otherThing = getOtherThing()
doFlow(something, record)
if (record.someType = Types.SOME_SPECIFIC_TYPE) {
doFlow(something, record)
}
}
def doFlow(Something something, Record record) {
when:
//code
then:
//asserts
when:
//code
and:
//mode code
then:
//code
}
Run Code Online (Sandbox Code Playgroud)
但是,在运行时,我得到:groovy.lang.MissingMethodException: No signature of method doFlow() is applicable for arguments Something, Record values: [given values].
我想编写一个运行的shell,直到将某些内容写入文件(由另一个进程).我写了这个:
PID_FILE=log.txt
DONE=0
while [$DONE -eq 0]
do
cat $PID_FILE | while read LINE
do
if [$LINE -neq ""]; then
echo "Do stuff here"
$DONE=1
fi
done
done
echo "DONE"
echo "">$PID_FILE
Run Code Online (Sandbox Code Playgroud)
但我明白了
test.sh: 3: test.sh: [0: not found
DONE
Run Code Online (Sandbox Code Playgroud) 我想收集一些统计数据,我想知道是否可以直接通过数据库查询获取一些最小值,最大值和平均值.
在过度简化中我有这样的事情:
Person | Account
__________________
Person1 | Account1
Person1 | Account2
Person1 | Account3
Person2 | Account4
Person2 | Account5
..................
Run Code Online (Sandbox Code Playgroud)
我想查找一个人拥有的最大,最小和平均帐户数.这可能是SQL查询吗?