Skip to content

Instantly share code, notes, and snippets.

View sebersole's full-sized avatar

Steve Ebersole sebersole

View GitHub Profile
@sebersole
sebersole / NamedQueryRepository.java
Created March 26, 2013 15:40
NamedQueryRepository.java
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
<hibernate-mapping>
<class entity-name="IncomingPartnerData" table="PARTNER_INCOMING_DATA">
...
</class>
<class entity-name="IncomingCustomerData" table="CUSTOMER_INCOMING_DATA">
...
</class>
<hibernate-mapping>
<class name="IncomingData" entity-name="IncomingPartnerData" table="PARTNER_INCOMING_DATA">
...
</class>
<class name="IncomingData" entity-name="IncomingCustomerData" table="CUSTOMER_INCOMING_DATA">
...
</class>
pom.withXml {
def root = asNode();
root.appendNode( 'url', 'http://hibernate.org' )
def org = root.appendNode( 'organization' )
org.appendNode( 'name', 'Hibernate.org' )
org.appendNode( 'url', 'http://hibernate.org' )
def jira = root.appendNode( 'issueManagement' )
jira.appendNode( 'system', 'jira' )
jira.appendNode( 'url', 'https://hibernate.atlassian.net/browse/HHH' )
}
@sebersole
sebersole / gist:5357195
Last active December 16, 2015 01:39
"many-to-many" with association entity using annotations
@Entity
public class Membership implements IMembership {
@EmbeddedId
private MembershipKey id;
}
@Embeddable
public final class MembershipKey implements IMembershipKey, Serializable {
@ManyToOne
private User user;
@Entity
public class SomeHolderOfAttributes {
...
@OneToMany( ... )
@JoinColumn( ... )
@OrderColumn( ... )
@CollectionType( type=AtributeListDescriptor.class )
private List<Attribute> atributes;
@Entity
@Table(name = "ACTIVITY")
@Audited
public class Activity implements Serializable {
@Id
@Column(name = "Id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ManyToOne
class MyDialectResolver implements DialectResolver {
public Dialect resolveDialect(DatabaseMetaData metaData) {
if ( "H2".equals( metaData.getDatabaseProductName ) ) {
return MyH2Dialect.INSTANCE;
}
else if ( "PostgreSQL".equals( metaData.getDatabaseProductName ) ) {
return MyPgsqlDialect.INSTANCE;
}
return null;
}
@sebersole
sebersole / gist:5579237
Created May 14, 2013 20:26
Alternative getAppropriateUniqueKeyLoader impl
private EntityLoader getAppropriateUniqueKeyLoader(String propertyName, SessionImplementor session) {
final boolean useStaticLoader = !session.getLoadQueryInfluencers().hasEnabledFilters()
&& !session.getLoadQueryInfluencers().hasEnabledFetchProfiles()
//ugly little workaround for fact that createUniqueKeyLoaders() does not handle component properties
&& propertyName.indexOf( '.' ) < 0;
if ( useStaticLoader ) {
EntityLoader loader = (EntityLoader) uniqueKeyLoaders.get( propertyName );
if ( loader == null ) {
if ( propertyName.equals( entityMetamodel.getIdentifierProperty().getName() ) ) {
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations [DELETE FROM com.xxx.xx.xxx.domain.feed.Feed f WHERE f.id NOT IN (SELECT MAX(ff.id) FROM com.xxx.xx.xxx.domain.feed.Feed ff WHERE ff.person.id =:personId GROUP BY ff.date,ff.app,ff.unitType) AND f.person.id=:personId]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations [DELETE FROM com.xxx.xx.xxx.domain.feed.Feed f WHERE f.id NOT IN (SELECT MAX(ff.id) FROM com.xxx.xx.xxx.domain.feed.Feed ff WHERE ff.person.id =:personId GROUP BY ff.date,ff.app,ff.unitType) AND f.person.id=:personId]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:298) ~[spring-orm-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106) ~[spring-orm-3